常用Git命令清单
一般来说,日常使用只要记住下图6个命令,就可以了。但是熟练使用,恐怕要记住60~100个命令。

下面是我整理的常用 Git 命令清单。几个专用名词的译名如下。
- Workspace:工作区
- Index / Stage:暂存区
- Repository:仓库区(或本地仓库)
- Remote:远程仓库
 一、新建代码库
| 12
 3
 4
 5
 6
 7
 8
 
 | # 在当前目录新建一个Git代码库$ git init
 
 # 新建一个目录,将其初始化为Git代码库
 $ git init [project-name]
 
 # 下载一个项目和它的整个代码历史
 $ git clone [url]
 
 | 
 二、配置
Git的设置文件为.gitconfig,它可以在用户主目录下(全局配置),也可以在项目目录下(项目配置)。
| 12
 3
 4
 5
 6
 7
 8
 9
 
 | # 显示当前的Git配置$ git config --list
 
 # 编辑Git配置文件
 $ git config -e [--global]
 
 # 设置提交代码时的用户信息
 $ git config [--global] user.name "[name]"
 $ git config [--global] user.email "[email address]"
 
 | 
 三、增加/删除文件
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 
 | 
 $ git add [file1] [file2] ...
 
 
 $ git add [dir]
 
 
 $ git add .
 
 
 
 $ git add -p
 
 
 $ git rm [file1] [file2] ...
 
 
 $ git rm --cached [file]
 
 
 $ git mv [file-original] [file-renamed]
 
 | 
 四、代码提交
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 
 | $ git commit -m [message]
 
 
 $ git commit [file1] [file2] ... -m [message]
 
 
 $ git commit -a
 
 
 $ git commit -v
 
 
 
 $ git commit --amend -m [message]
 
 
 $ git commit --amend [file1] [file2] ...
 
 | 
 五、分支
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 
 | $ git branch
 
 
 $ git branch -r
 
 
 $ git branch -a
 
 
 $ git branch [branch-name]
 
 
 $ git checkout -b [branch]
 
 
 $ git branch [branch] [commit]
 
 
 $ git branch --track [branch] [remote-branch]
 
 
 $ git checkout [branch-name]
 
 
 $ git checkout -
 
 
 $ git branch --set-upstream [branch] [remote-branch]
 
 
 $ git merge [branch]
 
 
 $ git cherry-pick [commit]
 
 
 $ git branch -d [branch-name]
 
 
 $ git push origin --delete [branch-name]
 $ git branch -dr [remote/branch]
 
 | 
 六、标签
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 
 | $ git tag
 
 
 $ git tag [tag]
 
 
 $ git tag [tag] [commit]
 
 
 $ git tag -d [tag]
 
 
 $ git push origin :refs/tags/[tagName]
 
 
 $ git show [tag]
 
 
 $ git push [remote] [tag]
 
 
 $ git push [remote] --tags
 
 
 $ git checkout -b [branch] [tag]
 
 | 
 七、查看信息
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 
 | $ git status
 
 
 $ git log
 
 
 $ git log --stat
 
 
 $ git log -S [keyword]
 
 
 $ git log [tag] HEAD --pretty=format:%s
 
 
 $ git log [tag] HEAD --grep feature
 
 
 $ git log --follow [file]
 $ git whatchanged [file]
 
 
 $ git log -p [file]
 
 
 $ git log -5 --pretty --oneline
 
 
 $ git shortlog -sn
 
 
 $ git blame [file]
 
 
 $ git diff
 
 
 $ git diff --cached [file]
 
 
 $ git diff HEAD
 
 
 $ git diff [first-branch]...[second-branch]
 
 
 $ git diff --shortstat "@{0 day ago}"
 
 
 $ git show [commit]
 
 
 $ git show --name-only [commit]
 
 
 $ git show [commit]:[filename]
 
 
 $ git reflog
 
 | 
 八、远程同步
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 
 | $ git fetch [remote]
 
 
 $ git remote -v
 
 
 $ git remote show [remote]
 
 
 $ git remote add [shortname] [url]
 
 
 $ git pull [remote] [branch]
 
 
 $ git push [remote] [branch]
 
 
 $ git push [remote] --force
 
 
 $ git push [remote] --all
 
 | 
 九、撤销
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 
 | $ git checkout [file]
 
 
 $ git checkout [commit] [file]
 
 
 $ git checkout .
 
 
 $ git reset [file]
 
 
 $ git reset --hard
 
 
 $ git reset [commit]
 
 
 $ git reset --hard [commit]
 
 
 $ git reset --keep [commit]
 
 
 
 $ git revert [commit]
 
 
 $ git stash
 $ git stash pop
 
 | 
 十、常用操作组合
 1. 修改本地分支名和远程分支名
| 12
 3
 
 | git branch -m old_branch new_branch git push origin :old_branch
 git push --set-upstream origin new_branch
 
 | 
相关文章:
《如何撤销 Git 操作?》
《git cherry-pick 教程》 复制某分支上的部分提交到另一个分支上(相对于可以选择指定提交的 rebase 操作)。
命令清单来源:https://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html
                
                __END__