常用Git命令清单
一般来说,日常使用只要记住下图6个命令,就可以了。但是熟练使用,恐怕要记住60~100个命令。
名次解释
下面是我整理的常用 Git 命令清单。
几个专用名词的译名如下: Workspace:
工作区 Index / Stage:
暂存区 Repository:
仓库区(或本地仓库) Remote:远程仓库
一、新建代码库
1 2 3 4 5 6
| # 在当前目录新建一个Git代码库 $ git init # 新建一个目录,将其初始化为Git代码库 $ git init [project-name] # 下载一个项目和它的整个代码历史 $ git clone [url]
|
二、配置
Git的设置文件为.gitconfig,它可以在用户主目录下(全局配置),也可以在项目目录下(项目配置)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| # 显示当前的Git配置 $ git config --list # 编辑Git配置文件 $ git config -e [--global] # 设置提交代码时的用户信息 $ git config [--global] user.name "[name]" $ git config [--global] user.email "[email address]" # 颜色设置 git config --global color.ui true # git status等命令自动着色 git config --global color.status auto git config --global color.diff auto git config --global color.branch auto git config --global color.interactive auto git config --global --unset http.proxy # remove proxy configuration on git
|
三、增加/删除文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| $ 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
| $ git commit -m [message]
$ git commit [file1] [file2] ... -m [message]
$ git commit -a
$ git commit -v
$ git commit -am 'message'
$ git commit --amend -m [message]
$ git commit --amend [file1] [file2] ...
|
五、分支
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
| $ 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]
$ git checkout v2.0
$ git checkout -b devel origin/develop
git checkout -- README
|
六、标签
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| # 列出所有tag $ git tag # 新建一个tag在当前commit $ git tag [tag] # 新建一个tag在指定commit $ git tag [tag] [commit] # 删除本地tag $ git tag -d [tag] # 删除远程tag $ git push origin :refs/tags/[tagName] # 查看tag信息 $ git show [tag] # 提交指定tag $ git push [remote] [tag] # 提交所有tag $ git push [remote] --tags # 新建一个分支,指向某个tag $ 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
| # 显示有变更的文件 $ git status # 显示当前分支的版本历史 $ git log # 显示commit历史,以及每次commit发生变更的文件 $ git log --stat # 搜索提交历史,根据关键词 $ git log -S [keyword] # 显示某个commit之后的所有变动,每个commit占据一行 $ git log [tag] HEAD --pretty=format:%s # 显示某个commit之后的所有变动,其"提交说明"必须符合搜索条件 $ git log [tag] HEAD --grep feature # 显示某个文件的版本历史,包括文件改名 $ git log --follow [file] $ git whatchanged [file] # 显示指定文件相关的每一次diff $ git log -p [file] # 显示过去5次提交 $ git log -5 --pretty --oneline # 显示所有提交过的用户,按提交次数排序 $ git shortlog -sn # 显示指定文件是什么人在什么时间修改过 $ git blame [file] # 显示暂存区和工作区的差异 $ git diff # 显示暂存区和上一个commit的差异 $ git diff --cached [file] # 显示工作区与当前分支最新commit之间的差异 $ 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
|
八、远程同步
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| $ 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
| $ 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 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| git init git config --global user.name "xxx" git config --global user.email "xxx@xxx.com" git config --global color.ui true git config --global color.status auto git config --global color.diff auto git config --global color.branch auto git config --global color.interactive auto git config --global --unset http.proxy git clone git+ssh://git@192.168.53.168/VT.git git status git add xyz git add . git commit -m 'xxx' git commit --amend -m 'xxx' git commit -am 'xxx' git rm xxx git rm -r * git log git log -1 git log -5 git log --stat git log -p -m git show dfb02e6e4f2f7b573337763e5c0013802e392818 git show dfb02 git show HEAD git show HEAD^ git tag git tag -a v2.0 -m 'xxx' git show v2.0 git log v2.0 git diff git diff --cached git diff HEAD^ git diff HEAD -- ./lib git diff origin/master..master git diff origin/master..master --stat git remote add origin git+ssh://git@192.168.53.168/VT.git git branch git branch --contains 50089 git branch -a git branch -r git branch --merged git branch --no-merged git branch -m master master_copy git checkout -b master_copy git checkout -b master master_copy git checkout features/performance git checkout --track hotfixes/BJVEP933 git checkout v2.0 git checkout -b devel origin/develop git checkout -- README git merge origin/master git cherry-pick ff44785404a8e git push origin master git push origin :hotfixes/BJVEP933 git push --tags git fetch git fetch --prune git pull origin master git mv README README2 git reset --hard HEAD git rebase git branch -d hotfixes/BJVEP933 git branch -D hotfixes/BJVEP933 git ls-files git show-branch git show-branch --all git whatchanged git revert dfb02e6e4f2f7b573337763e5c0013802e392818 git ls-tree HEAD git rev-parse v2.0 git reflog git show HEAD@{5} git show master@{yesterday} git log --pretty=format:'%h %s' --graph git show HEAD~3 git show -s --pretty=raw 2be7fcb476 git stash git stash list git stash show -p stash@{0} git stash apply stash@{0} git grep "delete from" git grep -e '#define' --and -e SORT_DIRENT git gc git fsck
$ git archive
|