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

下面是我整理的常用 Git 命令清单。几个专用名词的译名如下。
- Workspace:工作区
- Index / Stage:暂存区
- Repository:仓库区(或本地仓库)
- Remote:远程仓库
一、新建代码库
1 2 3 4 5 6 7 8
| $ git init
$ git init [project-name]
$ git clone [url]
|
二、配置
Git的设置文件为.gitconfig
,它可以在用户主目录下(全局配置),也可以在项目目录下(项目配置)。
1 2 3 4 5 6 7 8 9
| $ git config --list
$ git config -e [--global]
$ git config [--global] user.name "[name]" $ git config [--global] user.email "[email address]"
|
三、增加/删除文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| # 添加指定文件到暂存区 $ 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]
|
四、代码提交
1 2 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]
# 提交工作区自上次commit之后的变化,直接到仓库区 $ git commit -a
# 提交时显示所有diff信息 $ git commit -v
# 使用一次新的commit,替代上一次提交 # 如果代码没有任何新变化,则用来改写上一次commit的提交信息 $ git commit
# 重做上一次commit,并包括指定文件的新变化 $ git commit
|
五、分支
1 2 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]
|
六、标签
1 2 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]
|
七、查看信息
1 2 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
$ git log -S [keyword]
$ git log [tag] HEAD
$ git log [tag] HEAD
$ git log $ git whatchanged [file]
$ git log -p [file]
$ git log -5
$ git shortlog -sn
$ git blame [file]
$ git diff
$ git diff
$ git diff HEAD
$ git diff [first-branch]...[second-branch]
$ git diff
$ git show [commit]
$ git show
$ git show [commit]:[filename]
$ git reflog
|
八、远程同步
1 2 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
|
九、撤销
1 2 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]
# 恢复某个commit的指定文件到暂存区和工作区 $ git checkout [commit] [file]
# 恢复暂存区的所有文件到工作区 $ git checkout .
# 重置暂存区的指定文件,与上一次commit保持一致,但工作区不变 $ git reset [file]
# 重置暂存区与工作区,与上一次commit保持一致 $ git reset
# 重置当前分支的指针为指定commit,同时重置暂存区,但工作区不变 $ git reset [commit]
# 重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致 $ git reset
# 重置当前HEAD为指定commit,但保持暂存区和工作区不变 $ git reset
# 新建一个commit,用来撤销指定commit # 后者的所有变化都将被前者抵消,并且应用到当前分支 $ git revert [commit]
# 暂时将未提交的变化移除,稍后再移入 $ git stash $ git stash pop
|
十、其他
本文来自:http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html