Git 常用命令清单
一般来说,日常使用只要记住下图6个命令,就可以了。但是熟练使用,恐怕要记住60~100个命令。 下面是我整理的常用 Git 命令清单。几个专用名词的译名如下。
● Workspace:工作区
● Index / Stage:暂存区
● Repository:仓库区(或本地仓库)
● Remote:远程仓库
一、新建代码库
1. # 在当前目录新建一个Git代码库
2. $ git init
3.
4. # 新建一个目录,将其初始化为Git代码库
5. $ git init [project-name]
6.
7. # 下载一个项目和它的整个代码历史
8. $ git clone [url]
二、配置
Git的设置文件为.gitconfig,它可以在用户主目录下(全局配置),也可以在项目目录下(项目配置)。
1. # 显示当前的Git配置
2. $ git config --list
3.
4. # 编辑Git配置文件
5. $ git config -e [--global]
6.
7. # 设置提交代码时的用户信息
8. $ git config [--global] user.name "[name]"
9. $ git config [--global] user.email "[email address]"
三、增加/删除文件
1. # 添加指定文件到暂存区
2. $ git add [file1] [file2] ...
3.
4. # 添加指定目录到暂存区,包括子目录
5. $ git add [dir]
6.
7. # 添加当前目录的所有文件到暂存区
8. $ git add .
9.
10. # 添加每个变化前,都会要求确认
11. # 对于同一个文件的多处变化,可以实现分次提交
12. $ git add -p
13.
14. # 删除工作区文件,并且将这次删除放入暂存区
15. $ git rm [file1] [file2] ...
16.
17. # 停止追踪指定文件,但该文件会保留在工作区
18. $ git rm --cached [file]
19.
20. # 改名文件,并且将这个改名放入暂存区
21. $ git mv [file-original] [file-renamed]
四、代码提交
1. # 提交暂存区到仓库区
2. $ git commit -m [message]
3.
4. # 提交暂存区的指定文件到仓库区
5. $ git commit [file1] [file2] ... -m [message]
6.
7. # 提交工作区自上次commit之后的变化,直接到仓库区
8. $ git commit -a
9.
10. # 提交时显示所有diff信息
11. $ git commit -v
12.
13. # 使用一次新的commit,替代上一次提交
14. # 如果代码没有任何新变化,则用来改写上一次commit的提交信息
15. $ git commit --amend -m [message]
16.
17. # 重做上一次commit,并包括指定文件的新变化
18. $ git commit --amend [file1] [file2] ...
五、分支
1. # 列出所有本地分支
2. $ git branch
3.
4. # 列出所有远程分支
5. $ git branch -r
6.
7. # 列出所有本地分支和远程分支
8. $ git branch -a
9.
10. # 新建一个分支,但依然停留在当前分支
11. $ git branch [branch-name]
12.
13. # 新建一个分支,并切换到该分支
14. $ git checkout -b [branch]
15.
16. # 新建一个分支,指向指定commit
17. $ git branch [branch] [commit]
18.
19. # 新建一个分支,与指定的远程分支建立追踪关系
20. $ git branch --track [branch] [remote-branch]
21.
22. # 切换到指定分支,并更新工作区
23. $ git checkout [branch-name]
24.
25. # 切换到上一个分支
26. $ git checkout -
27.
28. # 建立追踪关系,在现有分支与指定的远程分支之间
29. $ git branch --set-upstream [branch] [remote-branch]
30.
31. # 合并指定分支到当前分支
32. $ git merge [branch]
33.
34. # 选择一个commit,合并进当前分支
35. $ git cherry-pick [commit]
36.
37. # 删除分支
38. $ git branch -d [branch-name]
39.
40. # 删除远程分支
41. $ git push origin --delete [branch-name]
42. $ git branch -dr [remote/branch]
六、标签
1. # 列出所有tag
2. $ git tag
3.
4. # 新建一个tag在当前commit
5. $ git tag [tag]
6.
7. # 新建一个tag在指定commit
8. $ git tag [tag] [commit]
9.
10. # 删除本地tag
11. $ git tag -d [tag]
12.
13. # 删除远程tag
14. $ git push origin :refs/tags/[tagName]
15.
16. # 查看tag信息
17. $ git show [tag]
18.
19. # 提交指定tag
20. $ git push [remote] [tag]
21.
22. # 提交所有tag
23. $ git push [remote] --tags
24.
25. # 新建一个分支,指向某个tag
26. $ git checkout -b [branch] [tag]
七、查看信息
1. # 显示有变更的文件
2. $ git status
3.
4. # 显示当前分支的版本历史
5. $ git log
6.
7. # 显示commit历史,以及每次commit发生变更的文件
8. $ git log --stat
9.
10. # 搜索提交历史,根据关键词
11. $ git log -S [keyword]
12.
13. # 显示某个commit之后的所有变动,每个commit占据一行
14. $ git log [tag] HEAD --pretty=format:%s
15.
16. # 显示某个commit之后的所有变动,其"提交说明"必须符合搜索条件
17. $ git log [tag] HEAD --grep feature
18.
19. # 显示某个文件的版本历史,包括文件改名
20. $ git log --follow [file]
21. $ git whatchanged [file]
22.
23. # 显示指定文件相关的每一次diff
24. $ git log -p [file]
25.
26. # 显示过去5次提交
27. $ git log -5 --pretty --oneline
28.
29. # 显示所有提交过的用户,按提交次数排序
30. $ git shortlog -sn
31.
32. # 显示指定文件是什么人在什么时间修改过
33. $ git blame [file]
34.
35. # 显示暂存区和工作区的差异
36. $ git diff
37.
38. # 显示暂存区和上一个commit的差异
39. $ git diff --cached [file]
40.
41. # 显示工作区与当前分支最新commit之间的差异
42. $ git diff HEAD
43.
44. # 显示两次提交之间的差异
45. $ git diff [first-branch]...[second-branch]
46.
47. # 显示今天你写了多少行代码
48. $ git diff --shortstat "@{0 day ago}"
49.
50. # 显示某次提交的元数据和内容变化
51. $ git show [commit]
52.
53. # 显示某次提交发生变化的文件
54. $ git show --name-only [commit]
55.
56. # 显示某次提交时,某个文件的内容
57. $ git show [commit]:[filename]
58.
59. # 显示当前分支的最近几次提交
60. $ git reflog
八、远程同步
1. # 下载远程仓库的所有变动
2. $ git fetch [remote]
3.
4. # 显示所有远程仓库
5. $ git remote -v
6.
7. # 显示某个远程仓库的信息
8. $ git remote show [remote]
9.
10. # 增加一个新的远程仓库,并命名
11. $ git remote add [shortname] [url]
12.
13. # 取回远程仓库的变化,并与本地分支合并
14. $ git pull [remote] [branch]
15.
16. # 上传本地指定分支到远程仓库
17. $ git push [remote] [branch]
18.
19. # 强行推送当前分支到远程仓库,即使有冲突
20. $ git push [remote] --force
21.
22. # 推送所有分支到远程仓库
23. $ git push [remote] --all
九、撤销
1. # 恢复暂存区的指定文件到工作区
2. $ git checkout [file]
3.
4. # 恢复某个commit的指定文件到暂存区和工作区
5. $ git checkout [commit] [file]
6.
7. # 恢复暂存区的所有文件到工作区
8. $ git checkout .
9.
10. # 重置暂存区的指定文件,与上一次commit保持一致,但工作区不变
11. $ git reset [file]
12.
13. # 重置暂存区与工作区,与上一次commit保持一致
14. $ git reset --hard
15.
16. # 重置当前分支的指针为指定commit,同时重置暂存区,但工作区不变
17. $ git reset [commit]
18.
19. # 重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致
20. $ git reset --hard [commit]
21.
22. # 重置当前HEAD为指定commit,但保持暂存区和工作区不变
23. $ git reset --keep [commit]
24.
25. # 新建一个commit,用来撤销指定commit
26. # 后者的所有变化都将被前者抵消,并且应用到当前分支
27. $ git revert [commit]
28.
29. # 暂时将未提交的变化移除,稍后再移入
30. $ git stash
31. $ git stash pop
十、其他
1. # 生成一个可供发布的压缩包
2. $ git archive
运行git config core.ignorecase false,关闭git忽略大小写配置,即可检测到大小写名称更改
1、常用的Git命令
|
命令 |
简要说明 |
|
git add |
添加至暂存区 |
|
git add–interactive |
交互式添加 |
|
git apply |
应用补丁 |
|
git am |
应用邮件格式补丁 |
|
git annotate |
同义词,等同于 git blame |
|
git archive |
文件归档打包 |
|
git bisect |
二分查找 |
|
git blame |
文件逐行追溯 |
|
git branch |
分支管理 |
|
git cat-file |
版本库对象研究工具 |
|
git checkout |
检出到工作区、切换或创建分支 |
|
git cherry-pick |
提交拣选 |
|
git citool |
图形化提交,相当于 git gui 命令 |
|
git clean |
清除工作区未跟踪文件 |
|
git clone |
克隆版本库 |
|
git commit |
提交 |
|
git config |
查询和修改配置 |
|
git describe |
通过里程碑直观地显示提交ID |
|
git diff |
差异比较 |
|
git difftool |
调用图形化差异比较工具 |
|
git fetch |
获取远程版本库的提交 |
|
git format-patch |
创建邮件格式的补丁文件。参见 git am 命令 |
|
git grep |
文件内容搜索定位工具 |
|
git gui |
基于Tcl/Tk的图形化工具,侧重提交等操作 |
|
git help |
帮助 |
|
git init |
版本库初始化 |
|
git init-db* |
同义词,等同于 git init |
|
git log |
显示提交日志 |
|
git merge |
分支合并 |
|
git mergetool |
图形化冲突解决 |
|
git mv |
重命名 |
|
git pull |
拉回远程版本库的提交 |
|
git push |
推送至远程版本库 |
|
git rebase |
分支变基 |
|
git rebase–interactive |
交互式分支变基 |
|
git reflog |
分支等引用变更记录管理 |
|
git remote |
远程版本库管理 |
|
git repo-config* |
同义词,等同于 git config |
|
git reset |
重置改变分支“游标”指向 |
|
git rev-parse |
将各种引用表示法转换为哈希值等 |
|
git revert |
反转提交 |
|
git rm |
删除文件 |
|
git show |
显示各种类型的对象 |
|
git stage* |
同义词,等同于 git add |
|
git stash |
保存和恢复进度 |
|
git status |
显示工作区文件状态 |
|
git tag |
里程碑管理 |
2、对象库操作相关命令
|
命令 |
简要说明 |
|
git commit-tree |
从树对象创建提交 |
|
git hash-object |
从标准输入或文件计算哈希值或创建对象 |
|
git ls-files |
显示工作区和暂存区文件 |
|
git ls-tree |
显示树对象包含的文件 |
|
git mktag |
读取标准输入创建一个里程碑对象 |
|
git mktree |
读取标准输入创建一个树对象 |
|
git read-tree |
读取树对象到暂存区 |
|
git update-index |
工作区内容注册到暂存区及暂存区管理 |
|
git unpack-file |
创建临时文件包含指定 blob 的内容 |
|
git write-tree |
从暂存区创建一个树对象 |
3、引用操作相关命令
|
命令 |
简要说明 |
|
git check-ref-format |
检查引用名称是否符合规范 |
|
git for-each-ref |
引用迭代器,用于shell编程 |
|
git ls-remote |
显示远程版本库的引用 |
|
git name-rev |
将提交ID显示为友好名称 |
|
git peek-remote* |
过时命令,请使用 git ls-remote |
|
git rev-list |
显示版本范围 |
|
git show-branch |
显示分支列表及拓扑关系 |
|
git show-ref |
显示本地引用 |
|
git symbolic-ref |
显示或者设置符号引用 |
|
git update-ref |
更新引用的指向 |
|
git verify-tag |
校验 GPG 签名的Tag |
4、版本库管理相关命令
|
命令 |
简要说明 |
|
git count-objects |
显示松散对象的数量和磁盘占用 |
|
git filter-branch |
版本库重构 |
|
git fsck |
对象库完整性检查 |
|
git fsck-objects* |
同义词,等同于 git fsck |
|
git gc |
版本库存储优化 |
|
git index-pack |
从打包文件创建对应的索引文件 |
|
git lost-found* |
过时,请使用 git fsck –lost-found 命令 |
|
git pack-objects |
从标准输入读入对象ID,打包到文件 |
|
git pack-redundant |
查找多余的 pack 文件 |
|
git pack-refs |
将引用打包到 .git/packed-refs 文件中 |
|
git prune |
从对象库删除过期对象 |
|
git prune-packed |
将已经打包的松散对象删除 |
|
git relink |
为本地版本库中相同的对象建立硬连接 |
|
git repack |
将版本库未打包的松散对象打包 |
|
git show-index |
读取包的索引文件,显示打包文件中的内容 |
|
git unpack-objects |
从打包文件释放文件 |
|
git verify-pack |
校验对象库打包文件 |
5、数据传输相关命令
|
命令 |
简要说明 |
|
|
git fetch-pack |
执行 git fetch 或 git pull 命令时在本地执行此命令,用于从其他版本库获取缺失的对象 |
|
|
git receive-pack |
执行 git push 命令时在远程执行的命令,用于接受推送的数据 |
|
|
git send-pack |
执行 git push 命令时在本地执行的命令,用于向其他版本库推送数据 |
|
|
git upload-archive |
执行 git archive –remote 命令基于远程版本库创建归档时,远程版本库执行此命令传送归档 |
|
|
git upload-pack |
执行 git fetch 或 git pull 命令时在远程执行此命令,将对象打包、上传 |
|
6、邮件相关命令
|
命令 |
简要说明 |
|
git imap-send |
将补丁通过 IMAP 发送 |
|
git mailinfo |
从邮件导出提交说明和补丁 |
|
git mailsplit |
将 mbox 或 Maildir 格式邮箱中邮件逐一提取为文件 |
|
git request-pull |
创建包含提交间差异和执行PULL操作地址的信息 |
|
git send-email |
发送邮件 |
7、协议相关命令
|
命令 |
简要说明 |
|
git daemon |
实现Git协议 |
|
git http-backend |
实现HTTP协议的CGI程序,支持智能HTTP协议 |
|
git instaweb |
即时启动浏览器通过 gitweb 浏览当前版本库 |
|
git shell |
受限制的shell,提供仅执行Git命令的SSH访问 |
|
git update-server-info |
更新哑协议需要的辅助文件 |
|
git http-fetch |
通过HTTP协议获取版本库 |
|
git http-push |
通过HTTP/DAV协议推送 |
|
git remote-ext |
由Git命令调用,通过外部命令提供扩展协议支持 |
|
git remote-fd |
由Git命令调用,使用文件描述符作为协议接口 |
|
git remote-ftp |
由Git命令调用,提供对FTP协议的支持 |
|
git remote-ftps |
由Git命令调用,提供对FTPS协议的支持 |
|
git remote-http |
由Git命令调用,提供对HTTP协议的支持 |
|
git remote-https |
由Git命令调用,提供对HTTPS协议的支持 |
|
git remote-testgit |
协议扩展示例脚本 |
8、版本库转换和交互相关命令
|
命令 |
简要说明 |
|
git archimport |
导入Arch版本库到Git |
|
git bundle |
提交打包和解包,以便在不同版本库间传递 |
|
git cvsexportcommit |
将Git的一个提交作为一个CVS检出 |
|
git cvsimport |
导入CVS版本库到Git。或者使用 cvs2git |
|
git cvsserver |
Git的CVS协议模拟器,可供CVS命令访问Git版本库 |
|
git fast-export |
将提交导出为 git-fast-import 格式 |
|
git fast-import |
其他版本库迁移至Git的通用工具 |
|
git svn |
Git 作为前端操作 Subversion |
9、合并相关的辅助命令
|
命令 |
简要说明 |
|
git merge-base |
供其他脚本调用,找到两个或多个提交最近的共同祖先 |
|
git merge-file |
针对文件的两个不同版本执行三向文件合并 |
|
git merge-index |
对index中的冲突文件调用指定的冲突解决工具 |
|
git merge-octopus |
合并两个以上分支。参见 git merge 的octopus合并策略 |
|
git merge-one-file |
由 git merge-index 调用的标准辅助程序 |
|
git merge-ours |
合并使用本地版本,抛弃他人版本。参见 git merge 的ours合并策略 |
|
git merge-recursive |
针对两个分支的三向合并。参见 git merge 的recursive合并策略 |
|
git merge-resolve |
针对两个分支的三向合并。参见 git merge 的resolve合并策略 |
|
git merge-subtree |
子树合并。参见 git merge 的 subtree 合并策略 |
|
git merge-tree |
显式三向合并结果,不改变暂存区 |
|
git fmt-merge-msg |
供执行合并操作的脚本调用,用于创建一个合并提交说明 |
|
git rerere |
重用所记录的冲突解决方案 |
10、 杂项
|
命令 |
简要说明 |
|
git bisect–helper |
由 git bisect 命令调用,确认二分查找进度 |
|
git check-attr |
显示某个文件是否设置了某个属性 |
|
git checkout-index |
从暂存区拷贝文件至工作区 |
|
git cherry |
查找没有合并到上游的提交 |
|
git diff-files |
比较暂存区和工作区,相当于 git diff –raw |
|
git diff-index |
比较暂存区和版本库,相当于 git diff –cached –raw |
|
git diff-tree |
比较两个树对象,相当于 git diff –raw A B |
|
git difftool–helper |
由 git difftool 命令调用,默认要使用的差异比较工具 |
|
git get-tar-commit-id |
从 git archive 创建的 tar 包中提取提交ID |
|
git gui–askpass |
命令 git gui 的获取用户口令输入界面 |
|
git notes |
提交评论管理 |
|
git patch-id |
补丁过滤行号和空白字符后生成补丁唯一ID |
|
git quiltimport |
将Quilt补丁列表应用到当前分支 |
|
git replace |
提交替换 |
|
git shortlog |
对 git log 的汇总输出,适合于产品发布说明 |
|
git stripspace |
删除空行,供其他脚本调用 |
|
git submodule |
子模组管理 |
|
git tar-tree |
过时命令,请使用 git archive |
|
git var |
显示 Git 环境变量 |
|
git web–browse |
启动浏览器以查看目录或文件 |
|
git whatchanged |
显示提交历史及每次提交的改动 |
|
git-mergetool–lib |
包含于其他脚本中,提供合并/差异比较工具的选择和执行 |
|
git-parse-remote |
包含于其他脚本中,提供操作远程版本库的函数 |
|
git-sh-setup |
包含于其他脚本中,提供 shell 编程的函数库 |
参考:git常用命令速查表