经常用到 Git,但是很多命令记不住,将其整理于此。(大量摘自网络)
一般来说,日常使用只要记住下图 6 个命令,就可以了。但是熟练使用,恐怕要要记住 60~100 个命令。
![git常用命令]()
下面整理的 Git 命令清单。几个专业名词的译名如下。
|  | Workspace:工作区 | 
|  | Index / Stage:暂存区 | 
|  | Repository:仓库区(本地仓库) | 
|  | Remote:远程仓库 | 
# 新建版本仓库
|  |  | 
|  | $ git init | 
|  |  | 
|  |  | 
|  | $ git init [project-name] | 
|  |  | 
|  |  | 
|  | $ git clone [-o faker] [url] | 
# 配置
Git 的设置文件为 <code>.gitconfig</code>,它可以在用户主目录下(全局配置),也可以在项目目录下(项目配置)。
|  |  | 
|  | $ git config --list | 
|  |  | 
|  |  | 
|  | $ git config -e [--global] | 
|  |  | 
|  |  | 
|  | $ git config [--global] user.name "[name]" | 
|  | $ git config [--global] user.email "[email address]" | 
|  |  | 
|  |  | 
|  | $ git config core.ignorecase  false | 
# 增加 / 删除文件
|  |  | 
|  | $ 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] | 
# 代码提交
|  |  | 
|  | $ 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] ... | 
# 分支
|  |  | 
|  | $ git branch | 
|  |  | 
|  |  | 
|  | $ git branch -r | 
|  |  | 
|  |  | 
|  | $ git branch -a | 
|  |  | 
|  |  | 
|  | $ git branch -v | 
|  |  | 
|  |  | 
|  | $ git branch -vv | 
|  |  | 
|  |  | 
|  | $ git branch --merged | 
|  |  | 
|  |  | 
|  | $ git branch --no-merged | 
|  |  | 
|  |  | 
|  | $ git branch [branch-name] | 
|  |  | 
|  |  | 
|  | $ git checkout -b [branch] | 
|  |  | 
|  |  | 
|  | $ git checkout --track [branch-name] | 
|  |  | 
|  |  | 
|  | $ git branch [branch] [commit] | 
|  |  | 
|  |  | 
|  | $ git branch --track [branch] [remote-branch] | 
|  |  | 
|  |  | 
|  | $ git checkout [branch-name] | 
|  |  | 
|  |  | 
|  | $ git checkout - | 
|  |  | 
|  |  | 
|  | $ git branch --set-upstream-to=[remote-branch] | 
|  | $ git branch --set-upstream [branch] [remote-branch]  | 
|  |  | 
|  |  | 
|  | $ git merge [branch] | 
|  |  | 
|  |  | 
|  | $ git merge --abort | 
|  |  | 
|  |  | 
|  | $ git cherry-pick [commit] | 
|  |  | 
|  |  | 
|  | $ git branch -d [branch-name] | 
|  |  | 
|  |  | 
|  | $ git push origin [branch-name] | 
|  |  | 
|  |  | 
|  | $ git push origin --delete [branch-name] | 
|  | $ git branch -dr [remote/branch] | 
# 标签
|  |  | 
|  | $ 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] | 
# 查看信息 / 搜索
|  |  | 
|  | $ git status [-sb]  | 
|  |  | 
|  |  | 
|  | $ 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 log [分支1]..[分支2] | 
|  | $ git log ^[分支1] [分支2] | 
|  | $ git log [分支2] --not [分支1] | 
|  |  | 
|  |  | 
|  | $ git log [分支1]...[分支2] | 
|  |  | 
|  |  | 
|  | $ 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 | 
|  |  | 
|  |  | 
|  | $ git grep -n [关键字] | 
|  |  | 
|  |  | 
|  | $ git grep --count [关键字] | 
|  |  | 
|  |  | 
|  | $ git grep --break --heading [关键字] | 
|  |  | 
|  |  | 
|  | $ git log -SiCheck --oneline | 
|  |  | 
|  |  | 
|  | $ git log -L :git_deflate_bound:zlib.c | 
# 远程同步
|  |  | 
|  | $ git fetch [shortname] | 
|  |  | 
|  |  | 
|  | $ git remote -v | 
|  |  | 
|  |  | 
|  | $ git ls-remote [shortname] | 
|  |  | 
|  |  | 
|  | $ git remote show [shortname] | 
|  |  | 
|  |  | 
|  | $ git remote add [shortname] [url] | 
|  |  | 
|  |  | 
|  | $ git remote rename [旧仓库名] [新仓库名] | 
|  |  | 
|  |  | 
|  | $ git remote rm [shortname] [url] | 
|  | $ git remote remove [shortname] [url] | 
|  |  | 
|  |  | 
|  | $ git remote set-url origin [shortname] [url] | 
|  |  | 
|  |  | 
|  | $ git pull [remote] [branch] | 
|  |  | 
|  |  | 
|  | $ git push [remote] [branch] | 
|  |  | 
|  |  | 
|  | $ git push [remote] --force | 
|  |  | 
|  |  | 
|  | $ git push [remote] --all | 
# 撤销
|  |  | 
|  | $ git checkout [file] | 
|  |  | 
|  |  | 
|  | $ git checkout [commit] [file] | 
|  |  | 
|  |  | 
|  | $ git checkout . | 
|  |  | 
|  |  | 
|  | $ git reset <commit_id>    | 
|  | $ git reset --mixed <commit_id> | 
|  |  | 
|  |  | 
|  | $ git reset --soft <commit_id> | 
|  |  | 
|  |  | 
|  | $ git reset --hard <commit_id> | 
|  |  | 
|  |  | 
|  | $ git reset --hard HEAD | 
|  |  | 
|  |  | 
|  |  | 
|  | $ git revert [commit] | 
|  |  | 
|  |  | 
|  | $ git stash [save] | 
|  |  | 
|  |  | 
|  | $ git stash --keep-index | 
|  |  | 
|  |  | 
|  | $ git stash -u | 
|  | $ git stash --include-untracked | 
|  |  | 
|  |  | 
|  | $ git stash --patch | 
|  |  | 
|  |  | 
|  | $ git stash apply [stash@{2}] | 
|  |  | 
|  |  | 
|  | $ git stash apply [stash@{2}] --index | 
|  |  | 
|  |  | 
|  | $ git stash pop | 
|  |  | 
|  |  | 
|  | $ git stash drop [stash@{2}] | 
|  |  | 
|  |  | 
|  | $ git stash list | 
|  |  | 
|  |  | 
|  | $ git clean -f -d | 
# 其他