Git 备忘清单
1. 入门
1.1 创建存储库
# 创建一个新的本地存储库git init [项目名称]# 克隆存储库(代码仓库)git clone <git_url># 将存储库克隆到指定目录git clone <git_url> 指定目录
# 将存储库克隆到指定目录,并指定分支git clone <git_url>-b<分支名称> 指定目录
1.2 做出改变
# 在工作目录中显示修改后的文件,为您的下一次提交暂存git status
# 暂存文件,准备提交gitadd[file]# 暂存所有更改的文件,准备提交gitadd.# 将所有暂存文件提交到版本化历史记录git commit -m"commit message"# 将所有跟踪的文件提交到版本化历史记录git commit -am"commit message"# 取消暂存文件,保留文件更改git reset [file]# 将所有内容恢复到最后一次提交git reset --hard# 已更改但未暂存内容的差异gitdiff# 已 commited 但尚未提交的内容的差异gitdiff--staged# 在指定分支之前应用当前分支的任何提交git rebase [branch]
1.3 配置
# 设置将附加到您的提交和标签的名称git config --global user.name "name"# 设置将附加到您的提交和标签 tags 的电子邮件地址git config --global user.email "email"# 启用 Git 输出的一些着色git config --global color.ui auto
# 在文本编辑器中编辑全局配置文件git config --global--edit# 显示本地 repo 配置设置git config --list# 删除全局设置git config --global--unset<entry-name>
1.4 使用分支
# 列出所有本地分支git branch
# 列出所有分支,本地和远程git branch -av# 切换到 my_branch,并更新工作目录git checkout my_branch
# 创建并切换到新分支new_branchgit checkout -b new_branch
# 删除名为 my_branch 的分支git branch -d my_branch
# 将分支 A 合并到分支 Bgit checkout branchB
git merge branchA
# 标记当前提交git tag my_tag
# 从远程分支中创建并切换到本地分支git checkout -b<branch-name> origin/<branch-name>
1.5 临时提交
# 保存已修改和分阶段的更改git stash
# 列出隐藏文件更改的堆栈顺序git stash list
# 从存储堆栈顶部编写工作git stash pop
# 丢弃存储堆栈顶部的更改git stash drop
# 回到某个 stash 的状态git stash apply <stash@{n}># 删除所有的 stashgit stash clear
1.6 观察你的存储库
# 显示当前活动分支的提交历史git log
# 显示 branchA 上不在 branchB 上的提交git log branchB..branchA
# 显示更改文件的提交,即使跨重命名git log --follow[file]# 显示 branchA 中的内容与 branchB 中的内容的差异gitdiff branchB...branchA
# 以人类可读的格式显示 Git 中的任何对象git show [SHA]
1.7 忽略文件 .gitignore
文件
.gitignore
指定了
Git
应该忽略的 未跟踪的 文件
行首
#
全行注释,不支持行尾类注释 *(转义
\#
)*行首 **
!
**否定模式 *(转义
\!
)**匹配任意路径匹配任意多个字符?匹配任意一个字符doc/**匹配
doc
文件夹下的全部内容doc/**/a匹配任意深度路径下的
a
文件或文件夹/表示路径分隔符,不区分操作系统
/
结尾仅会匹配文件夹,否则会匹配文件和文件夹空行不匹配任何文件行尾空格默认被忽略,可使用
\
进行转义行首空格被正常处理,不会被忽略
当前
.gitignore
文件定义规则的优先级高于上级路径
.gitignore
定义规则的优先级;后定义的规则优先级高于前面定义规则的优先级
# 忽略当前目录 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
1.8 重构文件名
# 从工作目录中删除文件并暂存删除gitrm<filename># 从版本控制中删除文件但在本地保留文件gitrm--cached<filename># 更改文件名并准备提交gitmv<filename-orig><filename-renamed>
1.9 同步
# 从该 Git 远程获取所有分支git fetch [alias]# 将远程分支合并到当前分支以使其保持最新状态git merge [alias]/[branch]# 没有快进git merge --no-ff [alias]/[branch]# 仅快进git merge --ff-only [alias]/[branch]# 将本地分支提交传输到远程存储库分支git push [alias][branch]# 从跟踪远程分支获取并合并任何提交git pull
# 将另一个分支的一个特定提交合并到当前分支git cherry-pick [commit_id]
1.10 远程
# 添加一个 git URL 作为别名git remote add[alias][url]# 显示您设置的远程存储库的名称git remote
# 显示远程存储库的名称和 URLgit remote -v# 删除远程存储库git remote rm[remote repo name]# 更改 git repo 的 URLgit remote set-url origin [git_url]
1.11 跟踪路径更改
# 从项目中删除文件并暂存删除以进行提交gitrm[file]# 更改现有文件路径并暂存移动gitmv[existing-path][new-path]# 显示所有提交日志,并指示任何移动的路径git log --stat-M
1.12 .gitattributes
# 设置默认行为,以防人们没有设置 core.autocrlf
*text=auto
# 明确声明您希望始终规范化并在结帐时
# 转换为本机行结尾的文本文件
*.c text*.h text
# 声明在结帐时始终以 CRLF 行结尾的文件
*.sln text eol=crlf
# 表示所有真正二进制且不应修改的文件
*.png binary
*.jpg binary
计入存储库语言
# 标记或取消标记要根据存储库的语言统计数据而
# 忽略或默认隐藏差异的路径
search/index.json linguist-generated=true
# 以下属性统计 SQL 文件
*.sql linguist-detectable=true
# 从统计信息中排除
docs/formatter.rb linguist-documentation=false
# 将它们从统计信息中排除
special-vendored-path/* linguist-vendored
# 将所有 .rb 文件检测为 Java 文件
*.rb linguist-language=Java
1.13 git 配置 ssh 代理
$ cat ~/.ssh/config
Host gitlab.com
# 直接使用 sh**socks 提供的 socks5 代理端口
ProxyCommand nc-X5-x127.0.0.1:1080 %h %p
Host github.com
ProxyCommand nc-X5-x127.0.0.1:1080 %h %p
2. Commit
2.1 改写历史
# 重写最后的提交消息git commit --amend-m"new message"# 修改最新的提交而不更改提交消息git commit --amend --no-edit
2.2 在 commit log 中显示 GPG 签名
git log --show-signature
2.3 修改远程 Commit 记录
git rebase -i HEAD~3
# 表示要修改当前版本的倒数第三次状态# 将要更改的记录行首单词 pick 改为 edit
pick 96dc3f9 提交 commit 描述内容 1
pick f1cce8a 提交 commit 描述内容 2
pick 6293516 提交 commit 描述内容 3# Rebase eeb03a4..6293516 onto eeb03a4# (3 commands)## Commands:# p, pick = 使用提交# r, reword = 使用提交,但编辑提交消息# e, edit = 使用提交,但停止修改# s, squash = 使用提交,但融合到先前的提交中# f, fixup = 像 squash,但丢弃此提交的日志消息# x, exec = 使用 shell 运行命令(该行的其余部分)# d, drop = 删除提交
保存并退出,会弹出下面提示
# 您现在可以修改提交,使用## git commit --amend## 对更改感到满意后,运行## git rebase --continue## 1. 通过这条命令进入编辑更改 commit,保存退出git commit --amend# 2. 保存退出确认修改,继续执行下面命令,git rebase --continue# 如果修改多条记录反复执行上面两条命令直到完成所有修改# 最后,确保没有人提交进行推送,最好不要加 -f 强制推送git push -f origin master
2.4 Commit
# 重写最后的提交信息git commit -v--amend
2.5 撤销远程记录
# 撤销一条记录git reset --hard HEAD~1
# 强制同步到远程仓库git push -f origin HEAD:master
2.6 放弃本地修改内容
# 如果有的修改以及加入暂存区的话git reset --hard# 还原所有修改,不会删除新增的文件git checkout .# 下面命令会删除新增的文件git clean -xdf
2.7 把 A 分支的某一个 commit,放到 B 分支上
# 切换到 B 分支git checkout <B># 将 A 分支 <hash-id> 的内容 pick 到 B 分支git cherry-pick <hash-id>
2.8 重设第一个 commit
把所有的改动都重新放回工作区,并清空所有的 commit,这样就可以重新提交第一个
commit
了
git update-ref -d HEAD
2.9 回到远程仓库的状态
抛弃本地所有的修改,回到远程仓库的状态
git fetch --all&&git reset --hard origin/master
2.10 commit 历史中显示 Branch1 有的但是 Branch2 没有 commit
git log Branch1 ^Branch2
2.11 git 迁移
# 从原地址克隆一份裸版本库git clone --bare https://github.com/username/project.git
# 然后新建一个地址,比如一下
https://gitee.com/username/newproject.git
# 进入project.git这个全裸版本库,以镜像推送的方式上传代码到newproject上。cd project.git
git push --mirror https://gitee.com/username/newproject.git
# 使用新地址,直接 Clone 到本地就可以了。
$ git clone https://gitee.com/username/newproject.git
3. Git Submodule 子模块
3.1 添加子模块
git submodule add<仓库地址><子模块路径>
3.2 克隆包含子模块的仓库
git clone <repository_url>--recursive
3.3 更新子模块
git submodule update --remote
3.4 切换到子模块的特定提交
cd<path_to_submodule>git checkout <commit_hash>
3.5 查看当前仓库中的子模块
git submodule status
3.6 初始化子模块
git submodule init
3.7 切换到父仓库的特定提交,并更新子模块
cd..git checkout <commit_hash>git submodule update --remote
3.8 获取并切换子模块的最新标签
cd<path_to_submodule>git fetch --tagsgit checkout $(git describe --tags$(git rev-list --tags --max-count=1))
3.9 子模块递归
# 添加所有已存在的子模块git submodule foreach --recursivegit submodule add<repository_url># 更新所有子模块到最新提交git submodule foreach --recursivegit pull origin master
# 检出特定的子模块路径git submodule foreach --recursivegit checkout <branch_name># 获取仓库中的所有子模块变化git submodule foreach --recursivegit fetch
# 获取并合并子模块的远程分支git submodule foreach --recursivegit pull origin <branch_name># 将子模块还原到父仓库中的初始提交git submodule foreach --recursivegit checkout .# 获取子模块的更新并忽略本地修改git submodule foreach --recursivegit fetch --allgit submodule foreach --recursivegit reset --hard origin/master
3.10 获取子模块的最新提交
cd<path_to_submodule>git pull
3.11 删除子模块
git submodule deinit <path_to_submodule>gitrm<path_to_submodule>
3.12 切换子模块的分支
cd<path_to_submodule>git checkout <branch_name>
3.13 初始化并更新所有子模块
git submodule init
git submodule update
3.14 切换子模块的特定标签
cd<path_to_submodule>git checkout tags/<tag_name>
4. Config 设置
4.1 查看配置的信息
获取帮助信息,查看修改个人信息的参数
githelp config
4.2 忽略文件的权限变化
不再将文件的权限变化视作改动
git config core.fileMode false
4.3 配置自动换行
自动转换坑太大,提交到git是自动将换行符转换为
lf
git config --global core.autocrlf input
4.4 获取帮助信息
git config --list
4.5 中文乱码的解决方案
git config --global core.quotepath false
4.6 删除全局设置
git config --global--unset<entry-name>
4.7 配置 http 和 socks 代理
# 查看代理git config --global http.proxy
git config --global https.proxy
git config --global socks.proxy
# 设置代理# 适用于 privoxy 将 socks 协议转为 http 协议的 http 端口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
# 只对 github.com 设置代理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
# 取消 github.com 代理git config --global--unset http.https://github.com.proxy
git config --global--unset https.https://github.com.proxy
4.8 Git 别名
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
命令的时候就不用再输入一大段了,例如我们要查看状态,只需:
git st
4.9 设置大小写敏感
# 查看git 的设置git config --get core.ignorecase
# 设置大小写敏感git config core.ignorecase false# 远程有俩相同目录,通过这种方式清除掉,然后提交记录gitrm-r--cached<目录/文件>
5. Git 技巧
5.1 重命名分支
# 重命名为newgit branch -m<new>git branch -m<old><new>#重命名分支# 推送并重置git push origin -u<new># 删除远程分支git push origin --delete<old>#方法1git push origin :oldBranchName #方法2
5.2 Log
5.2.1 按内容搜索更改
git log -S'<a term in the source>'
5.2.2 显示特定文件随时间的变化
git log -p<file_name>
5.2.3 打印出很酷的日志可视化
git log --pretty=oneline --graph--decorate--all
5.3 分支
5.3.1 列出所有分支及其上游
git branch -vv
5.3.2 快速切换到上一个分支
git checkout -
5.3.3 只获取所有远程分支
git branch -r
5.3.4 从另一个分支签出单个文件
git checkout <branch> -- <file>
5.3.5 删除本地存在远程不存在的分支
git remote prune origin
5.4 获取最近一次提交的 Hash
git rev-parse HEAD # e10721cb8859b2c# 获取短 hashgit rev-parse --short HEAD # e10721c
5.5 删除已经合并到 master 的分支
git branch --merged master |grep-v'^\*\| master'|xargs-n1git branch -d
5.6 查看冲突文件列表
gitdiff --name-only --diff-filter=U
5.7 展示工作区的冲突文件列表
# 输出工作区和暂存区的 different (不同)。gitdiff# 还可以展示本地仓库中任意两个 commit 之间的文件变动:gitdiff<commit-id><commit-id>
5.8 展示暂存区和最近版本的不同
gitdiff--cached
5.9 展示暂存区、工作区和最近版本的不同
# 输出工作区、暂存区 和本地最近的版本(commit)的different(不同)。gitdiff HEAD
5.10 删除已经合并到 master 的分支
git branch --merged master |grep-v'^\*\| master'|xargs-n1git branch -d
5.11 关联远程分支
# 或者在 push 时加上 -u 参数git branch -u origin/mybranch
# 关联之后,git branch -vv 就可以展示关联的远程分支名了, 同时推送到远程仓库直接:git push,不需要指定远程仓库git push origin/mybranch -u
5.12 查看远程分支和本地分支的对应关系
git remote show origin
5.13 展示当前分支的最近的 tag
git describe --tags--abbrev=0
5.14 修改作者名
git commit --amend--author='Author Name <[email protected]>'
5.15 增加远程仓库
git remote add origin <remote-url>
5.16 列出所有远程仓库
git remote -v
5.17 从 stash 中拿出某个文件的修改
git checkout <stash@{n}> -- <file-path>
5.18 展示所有 tracked 的文件
git ls-files -t
5.19 展示所有 untracked 的文件
git ls-files --others
5.20 展示所有忽略的文件
git ls-files --others-i --exclude-standard
5.21 把某一个分支导出成一个文件
git bundle create <file><branch-name>
5.22 从包中导入分支
新建一个分支,分支内容就是上面
git bundle create
命令导出的内容
git clone repo.bundle <repo-dir>-b<branch-name>
5.23 执行 rebase 之前自动 stash
git rebase --autostash
5.24 从远程仓库根据 ID,拉下某一状态,到本地分支
git fetch origin pull/<id>/head:<branch-name>
5.25 详细展示一行中的修改
gitdiff --word-diff
5.26 清除 gitignore 文件中记录的文件
git clean -X-f
5.27 展示忽略的文件
git status --ignored
5.28 新建并切换到新分支上,同时这个分支没有任何 commit
相当于保存修改,但是重写 commit 历史
git checkout --orphan<branch-name>
5.29 展示任意分支某一文件的内容
git show <branch-name>:<file-name>
5.30 clone 最新一次提交
只会
clone
最近一次提交,将减少
clone
时间
git clone --depth=1 https://github.com/user/repo.git
5.31 忽略某个文件的改动
# 关闭 track 指定文件的改动,也就是 Git 将不会在记录这个文件的改动git update-index --assume-unchanged path/to/file
# 恢复 track 指定文件的改动git update-index --no-assume-unchanged path/to/file
5.32 以最后提交的顺序列出所有 Git 分支
# 最新的放在最上面git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads
5.33 把暂存区的指定 file 放到工作区中
# 不添加参数,默认是 -mixedgit reset <file-name>
5.34 配置 SSH 协议代理
# 对于使用 git@ 协议的,可以配置 socks5 代理# macOS 系统编辑 ~/.ssh/config 文件,添加这几行,设置 github 代理
Host github.com
ProxyCommand nc-X5-x127.0.0.1:1080 %h %p
5.35 Fork仓库同步上游仓库
# 设置上游仓库git remote add upstream https://github.com/jaywcjlove/reference.git
# 本地项目操作git fetch upstream # 获取上游仓库更新git stach # 暂存本地修改(如果有)git branch -a# 列出所有远程仓库地址(非必须)git rebase remotes/upstream/main # 使用远程仓库的提交记录来重写本地提交记录git push -f# 强制推送到远程(github)仓库git stach pop # 恢复暂存的本地修改(如果有
6. 统计查询
6.1 查看 git 上的个人代码量
# username 需要改成自己的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 }' -
6.2 统计每个人增删行数
git log --format='%aN'|sort-u|\whileread name;doecho-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
6.3 查看仓库提交者排名
# 这里是排名前十,也可以更改排名git log --pretty='%aN'|sort|uniq-c|sort-k1-n-r|head-n10
6.4 提交数统计
git log --oneline|wc-l
6.5 查看某段代码是谁写的
git blame <file-name>
6.5 查看两个星期内的改动
git whatchanged --since='2 weeks ago'
6.6 在 commit log 中查找相关内容
git log --all--grep='<given-text>'# 通过 grep 查找,given-text: 所需要查找的字段
6.7 Git 仓库的大小
git ls-files |xargs-rdu-hs
6.8 Git 仓库的总大小
git count-objects -vH
6.9 查询历史体积大的 10 个文件
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-n10|cut-c13-
版权归原作者 霜晨月c 所有, 如有侵权,请联系我们删除。