git 常用命令

123 阅读1分钟

1. 查看本地分支列表 

git branch

2.删除本地分支 

git branch -D [branch name] 

3.查看远程分支列表 

git branch -a 

4.删除远程分支 

git push origin --delete [branch name]

6.创建分支 | 基于某个版本创建分支 

git branch [branch name]

git checkout -b 分支名 SHA 

7.切换分支 

git checkout [branch name] 
git switch [branch name] 

8.本地切换远程分支 

git checkout -b 本地分支名 origin/远程分支 
git checkout -t origin/远程分支

9.提交代码 

1.git status 
2.git add .  或者 git add <file>  //加入git 缓存区 
3.git commit -m '提交代码'  推送本地git 库 
4.git pull origin 分支名  // 拉起远端代码合并 
5.git push origin 分支名  // 提交到远程 

10. 本地代码关联远程仓库

git remote add origin [link]

11. 版本回退 

git reset --hard HEAD^ 上一个版本
git reset --hard 版本号 
git push -f -u origin master 强制推送  不推荐 

12.版本日志 

git log 查看历史 
git reflog 查看命令历史 

13. 缓存区删除 

git rm --cached 文件路径 

14. 缓存区移除,不会更改文件 

git restore --staged <file>

15.设置默认提交 

git push --set-upstream origin test 

16. 好神奇 

git gc --prune=now 

17. 同步分支 

git remote prune origin 

18. 挑拣提交 

git cherry-pick commitid

19. 设置默认提交分支 

git branch --set-upstream-to=origin/dev-name dev-name

20.  设置git用户名/密码/邮箱

git config user.name "name"
git config user.password "123456"
git config user.email "12345678@qq.com"