本备忘单总结了常用的 Git 命令行指令,以供快速参考。
入门
创建存储库
BASH
1 2 3 4 5 6 7 8
| $ git init [项目名称]
$ git clone <git_url>
$ git clone <git_url> 指定目录
$ git clone <git_url> -b <分支名称> 指定目录
|
做出改变
BASH
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
$ git status
$ git add [file]
$ git add .
$ git commit -m "commit message"
$ git commit -am "commit message"
$ git reset [file]
$ git reset --hard
$ git diff
$ git diff --staged
$ git rebase [branch]
|
配置
BASH
1 2 3 4 5 6 7 8 9 10 11 12
| $ git config --global user.name "name"
$ git config --global user.email "email"
$ git config --global color.ui auto
$ git config --global --edit
$ git config --list
$ git config --global --unset <entry-name>
|
使用分支
BASH
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| $ git branch
$ git branch -av
$ git checkout my_branch
$ git checkout -b new_branch
$ git branch -d my_branch
$ git remote prune origin
$ git checkout branchB $ git merge branchA
$ git tag my_tag
|
从远程分支中创建并切换到本地分支
BASH
1
| $ git checkout -b <branch-name> origin/<branch-name>
|
临时提交
BASH
1 2 3 4 5 6 7 8 9 10 11 12
| $ git stash
$ git stash list
$ git stash pop
$ git stash drop
$ git stash apply <stash@{n}>
$ git stash clear
|
观察你的存储库
BASH
1 2 3 4 5 6 7 8 9 10
| $ git log
$ git log branchB..branchA
$ git log --follow [file]
$ git diff branchB...branchA
$ git show [SHA]
|
忽略文件 .gitignore
文件 .gitignore 指定了 Git 应该忽略的 未跟踪的 文件
| :- |
:- |
行首 # |
全行注释,不支持行尾类注释 (转义 \#) |
行首 ! |
否定模式 (转义 \!) |
** |
匹配任意路径 |
* |
匹配任意多个字符 |
? |
匹配任意一个字符 |
doc/** |
匹配 doc 文件夹下的全部内容 |
doc/**/a |
匹配任意深度路径下的 a 文件或文件夹 |
/ |
表示路径分隔符,不区分操作系统 |
/ 结尾 |
仅会匹配文件夹,否则会匹配文件和文件夹 |
| 空行 |
不匹配任何文件 |
| 行尾空格 |
默认被忽略,可使用\进行转义 |
| 行首空格 |
被正常处理,不会被忽略 |
当前 .gitignore 文件定义规则的优先级高于上级路径 .gitignore 定义规则的优先级;后定义的规则优先级高于前面定义规则的优先级
GITIGNORE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| # 忽略当前目录logs文件夹下的全部内容 /logs/ /logs/* /logs/** # 上述几条规则等效
# 忽略 Mac 系统文件,包括任意子路径下的同名文件(夹) .DS_store
# 忽略 node_modules 文件夹,包括任意子路径下的同名文件夹 node_modules/
# 忽略任意子路径下build、target文件夹, # 但不忽略src/main、src/test下的build、target文件夹 build/ !**/src/main/**/build/ !**/src/test/**/build/ target/ !**/src/main/**/target/ !**/src/test/**/target/
# 使用 ! 重新包含指定文件(夹) !logs/.gitkeep
|
重构文件名
BASH
1 2 3 4 5 6
| git rm <filename>
git rm --cached <filename>
git mv <filename-orig> <filename-renamed>
|
同步
从该 Git 远程获取所有分支
将远程分支合并到当前分支以使其保持最新状态
BASH
1 2 3 4 5
| $ git merge [alias]/[branch]
$ git merge --no-ff [alias]/[branch]
$ git merge --ff-only [alias]/[branch]
|
将本地分支提交传输到远程存储库分支
BASH
1
| $ git push [alias] [branch]
|
从跟踪远程分支获取并合并任何提交
将另一个分支的一个特定提交合并到当前分支
BASH
1
| $ git cherry-pick [commit_id]
|
远程
BASH
1 2 3 4 5 6 7 8 9 10
| $ git remote add [alias] [url]
$ git remote
$ git remote -v
$ git remote rm [remote repo name]
$ git remote set-url origin [git_url]
|
跟踪路径更改
从项目中删除文件并暂存删除以进行提交
更改现有文件路径并暂存移动
BASH
1
| $ git mv [existing-path] [new-path]
|
显示所有提交日志,并指示任何移动的路径
.gitattributes
INI
1 2 3 4 5 6 7 8 9 10 11
| * text=auto
*.c text *.h text
*.sln text eol=crlf
*.png binary *.jpg binary
|
计入存储库语言
INI
1 2 3 4 5 6 7 8 9 10 11
|
search/index.json linguist-generated=true
*.sql linguist-detectable=true
docs/formatter.rb linguist-documentation=false
special-vendored-path/* linguist-vendored
*.rb linguist-language=Java
|
git 配置 ssh 代理
BASH
1 2 3 4 5 6 7
| $ cat ~/.ssh/config Host gitlab.com
ProxyCommand nc -X 5 -x 127.0.0.1:1080 %h %p
Host github.com ProxyCommand nc -X 5 -x 127.0.0.1:1080 %h %p
|
Commit
改写历史
BASH
1 2 3 4
| $ git commit --amend -m "new message"
$ git commit --amend --no-edit
|
在 commit log 中显示 GPG 签名
BASH
1
| $ git log --show-signature
|
修改远程 Commit 记录
BASH
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| $ git rebase -i HEAD~3
pick 96dc3f9 提交 commit 描述内容 1 pick f1cce8a 提交 commit 描述内容 2 pick 6293516 提交 commit 描述内容 3
|
保存并退出,会弹出下面提示
BASH
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
$ git commit --amend
$ git rebase --continue
$ git push -f origin master
|
Commit
重写最后的提交信息
撤销远程记录
BASH
1 2 3 4
| $ git reset --hard HEAD~1
$ git push -f origin HEAD:master
|
放弃本地修改内容
BASH
1 2 3 4 5 6
| $ git reset --hard
$ git checkout .
$ git clean -xdf
|
把 A 分支的某一个 commit,放到 B 分支上
BASH
1 2 3 4
| $ git checkout <B>
$ git cherry-pick <hash-id>
|
重设第一个 commit
BASH
1
| $ git update-ref -d HEAD
|
把所有的改动都重新放回工作区,并清空所有的 commit,这样就可以重新提交第一个 commit 了
回到远程仓库的状态
BASH
1
| $ git fetch --all && git reset --hard origin/master
|
抛弃本地所有的修改,回到远程仓库的状态
commit 历史中显示 Branch1 有的但是 Branch2 没有 commit
BASH
1
| $ git log Branch1 ^Branch2
|
git 迁移
BASH
1 2 3 4 5 6 7 8 9
| $ git clone --bare https://github.com/username/project.git
$ https://gitee.com/username/newproject.git
$ cd project.git $ git push --mirror https://gitee.com/username/newproject.git
$ git clone https://gitee.com/username/newproject.git
|
Git Submodule 子模块
添加子模块
BASH
1
| $ git submodule add <仓库地址> <子模块路径>
|
克隆包含子模块的仓库
BASH
1
| $ git clone <repository_url> --recursive
|
更新子模块
BASH
1
| $ git submodule update --remote
|
切换到子模块的特定提交
BASH
1 2
| $ cd <path_to_submodule> $ git checkout <commit_hash>
|
查看当前仓库中的子模块
初始化子模块
切换到父仓库的特定提交,并更新子模块
BASH
1 2 3
| $ cd .. $ git checkout <commit_hash> $ git submodule update --remote
|
获取并切换子模块的最新标签
BASH
1 2 3
| $ cd <path_to_submodule> $ git fetch --tags $ git checkout $(git describe --tags $(git rev-list --tags --max-count=1))
|
子模块递归
BASH
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| $ git submodule foreach --recursive git submodule add <repository_url>
$ git submodule foreach --recursive git pull origin master
$ git submodule foreach --recursive git checkout <branch_name>
$ git submodule foreach --recursive git fetch
$ git submodule foreach --recursive git pull origin <branch_name>
$ git submodule foreach --recursive git checkout .
$ git submodule foreach --recursive git fetch --all $ git submodule foreach --recursive git reset --hard origin/master
|
获取子模块的最新提交
BASH
1 2
| $ cd <path_to_submodule> $ git pull
|
删除子模块
BASH
1 2
| $ git submodule deinit <path_to_submodule> $ git rm <path_to_submodule>
|
切换子模块的分支
BASH
1 2
| $ cd <path_to_submodule> $ git checkout <branch_name>
|
初始化并更新所有子模块
BASH
1 2
| $ git submodule init $ git submodule update
|
切换子模块的特定标签
BASH
1 2
| $ cd <path_to_submodule> $ git checkout tags/<tag_name>
|
Config 设置
查看配置的信息
获取帮助信息,查看修改个人信息的参数
忽略文件的权限变化
BASH
1
| git config core.fileMode false
|
不再将文件的权限变化视作改动
配置自动换行
BASH
1
| $ git config --global core.autocrlf input
|
自动转换坑太大,提交到git是自动将换行符转换为 lf
获取帮助信息
中文乱码的解决方案
BASH
1
| $ git config --global core.quotepath false
|
删除全局设置
BASH
1
| $ git config --global --unset <entry-name>
|
配置 http 和 socks 代理
BASH
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| $ git config --global http.proxy $ git config --global https.proxy $ git config --global socks.proxy
$ git config --global http.proxy http://127.0.0.1:1080 $ git config --global https.proxy http://127.0.0.1:1080 $ git config --global socks.proxy 127.0.0.1:1080
$ git config --global --unset http.proxy $ git config --global --unset https.proxy $ git config --global --unset socks.proxy
$ git config --global http.https://github.com.proxy socks5://127.0.0.1:1080 $ git config --global https.https://github.com.proxy socks5://127.0.0.1:1080
$ git config --global --unset http.https://github.com.proxy $ git config --global --unset https.https://github.com.proxy
|
Git 别名
BASH
1 2 3 4
| $ git config --global alias.co checkout $ git config --global alias.br branch $ git config --global alias.ci commit $ git config --global alias.st status
|
配置好后,再输入 git 命令的时候就不用再输入一大段了,例如我们要查看状态,只需:
也可以看看:更多别名
设置大小写敏感
BASH
1 2 3 4 5 6
| $ git config --get core.ignorecase
$ git config core.ignorecase false
$ git rm -r --cached <目录/文件>
|
Git 技巧
重命名分支
重命名为new
BASH
1 2
| $ git branch -m <new> $ git branch -m <old> <new>
|
推送并重置
BASH
1
| $ git push origin -u <new>
|
删除远程分支
BASH
1 2
| $ git push origin --delete <old> $ git push origin :oldBranchName
|
Log
按内容搜索更改
BASH
1
| $ git log -S'<a term in the source>'
|
显示特定文件随时间的变化
BASH
1
| $ git log -p <file_name>
|
打印出很酷的日志可视化
BASH
1
| $ git log --pretty=oneline --graph --decorate --all
|
分支
列出所有分支及其上游
快速切换到上一个分支
只获取所有远程分支
从另一个分支签出单个文件
BASH
1
| $ git checkout <branch> -- <file>
|
删除本地存在远程不存在的分支
BASH
1
| $ git remote prune origin
|
获取最近一次提交的 Hash
BASH
1 2 3
| $ git rev-parse HEAD
$ git rev-parse --short HEAD
|
删除已经合并到 master 的分支
BASH
1
| $ git branch --merged master | grep -v '^\*\| master' | xargs -n 1 git branch -d
|
查看冲突文件列表
BASH
1
| $ git diff --name-only --diff-filter=U
|
展示工作区的冲突文件列表
输出工作区和暂存区的 different (不同)。
还可以展示本地仓库中任意两个 commit 之间的文件变动:
BASH
1
| $ git diff <commit-id> <commit-id>
|
展示暂存区和最近版本的不同
展示暂存区、工作区和最近版本的不同
输出工作区、暂存区 和本地最近的版本(commit)的different(不同)。
删除已经合并到 master 的分支
BASH
1
| $ git branch --merged master | grep -v '^\*\| master' | xargs -n 1 git branch -d
|
关联远程分支
BASH
1
| $ git branch -u origin/mybranch
|
或者在 push 时加上 -u 参数
BASH
1
| git push origin/mybranch -u
|
关联之后,git branch -vv 就可以展示关联的远程分支名了, 同时推送到远程仓库直接:git push,不需要指定远程仓库
查看远程分支和本地分支的对应关系
BASH
1
| $ git remote show origin
|
展示当前分支的最近的 tag
BASH
1
| $ git describe --tags --abbrev=0
|
修改作者名
BASH
1
| $ git commit --amend --author='Author Name <email@address.com>'
|
增加远程仓库
BASH
1
| $ git remote add origin <remote-url>
|
列出所有远程仓库
从 stash 中拿出某个文件的修改
BASH
1
| $ git checkout <stash@{n}> -- <file-path>
|
展示所有 tracked 的文件
展示所有 untracked 的文件
展示所有忽略的文件
BASH
1
| $ git ls-files --others -i --exclude-standard
|
把某一个分支导出成一个文件
BASH
1
| $ git bundle create <file> <branch-name>
|
从包中导入分支
BASH
1
| $ git clone repo.bundle <repo-dir> -b <branch-name>
|
新建一个分支,分支内容就是上面 git bundle create 命令导出的内容
执行 rebase 之前自动 stash
BASH
1
| $ git rebase --autostash
|
从远程仓库根据 ID,拉下某一状态,到本地分支
BASH
1
| $ git fetch origin pull/<id>/head:<branch-name>
|
详细展示一行中的修改
清除 gitignore 文件中记录的文件
展示忽略的文件
新建并切换到新分支上,同时这个分支没有任何 commit
BASH
1
| $ git checkout --orphan <branch-name>
|
相当于保存修改,但是重写 commit 历史
展示任意分支某一文件的内容
BASH
1
| $ git show <branch-name>:<file-name>
|
clone 最新一次提交
BASH
1
| $ git clone --depth=1 https://github.com/user/repo.git
|
只会 clone 最近一次提交,将减少 clone 时间
忽略某个文件的改动
关闭 track 指定文件的改动,也就是 Git 将不会在记录这个文件的改动
BASH
1
| git update-index --assume-unchanged path/to/file
|
恢复 track 指定文件的改动
BASH
1
| git update-index --no-assume-unchanged path/to/file
|
以最后提交的顺序列出所有 Git 分支
BASH
1
| git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads
|
最新的放在最上面
把暂存区的指定 file 放到工作区中
不添加参数,默认是 -mixed
配置 SSH 协议代理
BASH
1 2 3 4
|
Host github.com ProxyCommand nc -X 5 -x 127.0.0.1:1080 %h %p
|
Fork仓库同步上游仓库
设置上游仓库
BASH
1
| $ git remote add upstream https://github.com/jaywcjlove/reference.git
|
本地项目操作
BASH
1 2 3 4 5 6
| $ git fetch upstream $ git stach $ git branch -a $ git rebase remotes/upstream/main $ git push -f $ git stach pop
|
统计查询
查看 git 上的个人代码量
BASH
1 2
| git log --author="username" --pretty=tformat: --numstat | awk \ '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -
|
统计每个人增删行数
BASH
1 2 3 4
| git log --format='%aN' | sort -u |\ while read name; do echo -en "$name\t";\ git log --author="$name" --pretty=tformat: --numstat | awk \ '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -; done
|
查看仓库提交者排名
这里是排名前十,也可以更改排名
BASH
1
| git log --pretty='%aN' | sort | uniq -c | sort -k1 -n -r | head -n 10
|
提交数统计
BASH
1
| git log --oneline | wc -l
|
查看某段代码是谁写的
blame 的意思为责怪,你懂的。
查看两个星期内的改动
BASH
1
| $ git whatchanged --since='2 weeks ago'
|
在 commit log 中查找相关内容
BASH
1
| $ git log --all --grep='<given-text>'
|
通过 grep 查找,given-text: 所需要查找的字段
Git 仓库的大小
BASH
1
| $ git ls-files | xargs -r du -hs
|
Git 仓库的总大小
查询历史体积大的 10 个文件
BASH
1
| $ git rev-list --objects --all | git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' | awk '/^blob/ {print substr($0,6)}' | sort --numeric-sort --key=2 --reverse | head -n 10 | cut -c 13-
|
Conventional Commmits
格式
TEXT
1 2 3 4 5 6 7 8 9 10 11 12
| <type>(<scope>): <short summary> │ │ │ │ │ └─⫸ 紧凑简短的描述,无需大写,也不需要用句号结尾 │ │ │ └─⫸ Commit 范围: animations|bazel|benchpress|common|compiler|compiler-cli|core| │ elements|forms|http|language-service|localize|platform-browser| │ platform-browser-dynamic|platform-server|router|service-worker| │ upgrade|zone.js|packaging|changelog|docs-infra|migrations|ngcc|ve| │ devtools.... │ └─⫸ Commit 类型: build|ci|doc|docs|feat|fix|perf|refactor|test website|chore|style|type|revert
|
常用
| 类型 |
描述 |
feat: |
新特性 |
fix(scope): |
修复 scope 中的 Bug |
feat!: / feat(scope)!: |
breaking change / 重构 API |
chore(deps): |
更新依赖 |
Commit 类型
| 类型 |
描述 |
build: |
变更影响的是构建系统或者外部依赖 (如: gulp, npm) |
ci: |
修改了 CI 配置文件或脚本 (如: Github Action, Travis) |
chore: |
【重要】 变更不影响源代码或测试(如更新了辅助工具、库等) |
docs: |
只修改了文档 |
feat: |
【重要】 一个新特性 |
fix: |
【重要】 修复了一个 Bug |
perf: |
增强性能的代码变更 |
refactor: |
并非修复 Bug 或添加新特性的代码变更 |
revert: |
回退代码 |
style: |
变更不影响一些有意义的代码 (如: 删除空格、格式化代码、添加分号等) |
test: |
添加测试代码或修正已有的测试 |
patch 补丁的使用
简单创建
BASH
1
| $ git diff commit_id > my.patch
|
简单应用
检查补丁
BASH
1 2
| $ git apply --check path/to/my.patch
|
使用补丁
TEXT
1 2
| # 检查通过使用该补丁 $ git apply < path/to/my.patch
|
若有冲突则解决
TEXT
1 2
| # 检查不通过可以自动合入patch不冲突的代码,同时保留冲突部分 $ git apply --reject path/to/my.patch
|
Email patch 方式
BASH
1 2 3 4 5 6 7 8 9
| $ git format-patch commit_id -n
$ git format-patch commit_id -1
$ git format-patch -3
$ git am < email.txt
|
另见