git备忘清单
入门
创建一个新的本地存储库
克隆存储库(代码仓库)
将存储库克隆到指定目录
1
| $ git clone <git_url> 指定目录
|
将存储库克隆到指定目录,并指定分支
1
| $ git clone <git_url> -b <分支名称> 指定目录
|
做出改变
在工作目录中显示修改后的文件,为您的下一次提交暂存
暂存文件,准备提交
暂存所有更改的文件,准备提交
将所有暂存文件提交到版本化历史记录
1
| $ git commit -m "commit message"
|
将所有跟踪的文件提交到版本化历史记录
1
| $ git commit -am "commit message"
|
取消暂存文件,保留文件更改
将所有内容恢复到最后一次提交
已更改但未暂存内容的差异
已 commited 但尚未提交的内容的差异
在指定分支之前应用当前分支的任何提交
配置
设置将附加到您的提交和标签的名称
1
| $ git config --global user.name "name"
|
设置将附加到您的提交和标签 tags 的电子邮件地址
1
| $ git config --global user.email "email"
|
启用 Git 输出的一些着色
1
| $ git config --global color.ui auto
|
在文本编辑器中编辑全局配置文件
1
| $ git config --global --edit
|
显示本地 repo
配置设置
删除全局设置
1
| $ git config --global --unset <entry-name>
|
使用分支
列出所有本地分支
列出所有分支,本地和远程
切换到 my_branch
,并更新工作目录
1
| $ git checkout my_branch
|
创建一个名为 new_branch
的新分支
1
| $ git checkout -b new_branch
|
删除名为 my_branch
的分支
1
| $ git branch -d my_branch
|
将分支 A
合并到分支 B
1 2
| $ git checkout branchB $ git merge branchA
|
标记当前提交
从远程分支中创建并切换到本地分支
1
| $ git checkout -b <branch-name> origin/<branch-name>
|
临时提交
保存已修改和分阶段的更改 列出隐藏文件更改的堆栈顺序
从存储堆栈顶部编写工作
丢弃存储堆栈顶部的更改 回到某个 stash 的状态
1
| $ git stash apply <stash@{n}>
|
删除所有的 stash
观察你的储存库
显示当前活动分支的提交历史
显示 branchA 上不在 branchB 上的提交
1
| $ git log branchB..branchA
|
显示更改文件的提交,即使跨重命名
1
| $ git log --follow [file]
|
显示 branchA 中的内容与 branchB 中的内容的差异
1
| $ git diff branchB...branchA
|
以人类可读的格式显示 Git 中的任何对象
忽略文件 .gitgnore
文件 .gitignore
指定了 Git
应该忽略的
未跟踪的 文件
行首 ! |
否定模式 (转义 \! ) |
** |
匹配任意路径 |
* |
匹配任意多个字符 |
? |
匹配任意一个字符 |
doc/** |
匹配 doc 文件夹下的全部内容 |
doc/**/a |
匹配任意深度路径下的 a 文件或文件夹 |
/ |
表示路径分隔符,不区分操作系统 |
/ 结尾 |
仅会匹配文件夹,否则会匹配文件和文件夹 |
空行 |
不匹配任何文件 |
行尾空格 |
默认被忽略,可使用\ 进行转义 |
行首空格 |
被正常处理,不会被忽略 |
当前 .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
|
重构文件名
从工作目录中删除文件并暂存删除
从版本控制中删除文件但在本地保留文件 1
| git rm --cached <filename>
|
更改文件名并准备提交 1
| git mv <filename-orig> <filename-renamed>
|
同步
从该 Git 远程获取所有分支
将远程分支合并到当前分支以使其保持最新状态
1 2 3 4 5
| $ git merge [alias]/[branch]
$ git merge --no-ff [alias]/[branch]
$ git merge --ff-only [alias]/[branch]
|
将本地分支提交传输到远程存储库分支
1
| $ git push [alias] [branch]
|
从跟踪远程分支获取并合并任何提交
将另一个分支的一个特定提交合并到当前分支
1
| $ git cherry-pick [commit_id]
|
远程
添加一个 git URL 作为别名
1
| $ git remote add [alias] [url]
|
显示您设置的远程存储库的名称
显示远程存储库的名称和 URL
删除远程存储库
1
| $ git remote rm [remote repo name]
|
更改 git repo 的 URL
1
| $ git remote set-url origin [git_url]
|
跟踪路径更改
从项目中删除文件并暂存删除以进行提交
更改现有文件路径并暂存移动
1
| $ git mv [existing-path] [new-path]
|
显示所有提交日志,并指示任何移动的路径
git配置ssh代理
1 2 3 4 5 6
| $ 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
|
Git技巧
重命名分支
重命名为new
1 2
| $ git branch -m <new> $ git branch -m <old> <new>
|
推送并重置
1
| $ git push origin -u <new>
|
删除远程分支
1 2
| $ git push origin --delete <old> $ git push origin :oldBranchName
|
Log
按内容搜索更改
1
| $ git log -S'<a term in the source>'
|
显示特定文件随时间的变化
1
| $ git log -p <file_name>
|
打印出很酷的日志可视化
1
| $ git log --pretty=oneline --graph --decorate --all
|
分支
列出所有分支及其上游
快速切换到上一个分支
只获取所有远程分支
从另一个分支签出单个文件
1
| $ git checkout <branch> -- <file>
|
删除本地存在远程不存在的分支
Commit
重写最后的提交信息
忽略文件的权限变化
1
| git config core.fileMode false
|
不再将文件的权限变化视作改动
Git别名
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
|
设置大小写敏感
1 2 3 4 5 6
| $ git config --get core.ignorecase
$ git config core.ignorecase false
$ git rm -r --cached <目录/文件>
|
修改远程Commit记录
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 rebase -i HEAD~3
pick 96dc3f9 提交 commit 描述内容 1 pick f1cce8a 提交 commit 描述内容 2 pick 6293516 提交 commit 描述内容 3
保存并退出,会弹出下面提示
$ git commit --amend
$ git rebase --continue
$ git push -f origin master
|
撤销远程记录
撤销一条记录
1
| $ git reset --hard HEAD~1
|
强制同步到远程仓库
1
| $ git push -f origin HEAD:master
|
### 放弃本地修改内容 如果有的修改以及加入暂存区的话
还原所有修改,不会删除新增的文件
下面命令会删除新增的文件
获取最近一次提交的Hash
获取短 hash
1
| $ git rev-parse --short HEAD
|
删除已经合并到master的分支
1
| $ git branch --merged master | grep -v '^\*\| master' | xargs -n 1 git branch -d
|
把A分支的某一个Commit,当到B分支上
1 2 3 4
| $ git checkout <B>
$ git cherry-pick <hash-id>
|
回到远程仓库的状态
1
| $ git fetch --all && git reset --hard origin/master
|
重设第一个Commit
1
| $ git update-ref -d HEAD
|
查看冲突文件列表
1
| $ git diff --name-only --diff-filter=U
|
展示工作区的冲突文件列表
输出工作区和暂存区的 different (不同)。
还可以展示本地仓库中任意两个 commit 之间的文件变动:
1
| $ git diff <commit-id> <commit-id>
|
展示暂存区和最近版本的不同
中文乱码的解决方案
1
| $ git config --global core.quotepath false
|
展示暂存区、工作区和最近版本的不同
输出工作区、暂存区 和本地最近的版本(commit)的different(不同)。
删除已经合并到master的分支
1
| $ git branch --merged master | grep -v '^\*\| master' | xargs -n 1 git branch -d
|
关联远程分支
1
| $ git branch -u origin/mybranch
|
或者在 push
时加上 -u
参数
1
| $ git push origin/mybranch -u
|
关联之后,git branch -vv
就可以展示关联的远程分支名了,
同时推送到远程仓库直接:git push
,不需要指定远程仓库
查看远程分支和本地分支的对应关系
1
| $ git remote show origin
|
展示当前分支的最近的tag
1
| $ git describe --tags --abbrev=0
|
查看某段代码是谁写的
修改作者名
1
| $ git commit --amend --author='Author Name <email@address.com>'
|
修改远程仓库的Url
1
| $ git remote set-url origin <URL>
|
添加远程仓库
1
| $ git remote add origin <remote-url>
|
列出所有远程仓库
1
| $ git remote add origin <remote-url>
|
查看两个星期内的改动
1
| git whatchanged --since='2 weeks ago'
|
从stash中拿出某个文件的修改
1
| $ git checkout <stash@{n}> -- <file-path>
|
展示所有tracked的文件
展示所有untracked的文件
展示所有忽略的文件
1
| $ git ls-files --others -i --exclude-standard
|
把某一个分支导出成一个文件
1
| $ git bundle create <file> <branch-name>
|
从包中导入分支
1
| $ git clone repo.bundle <repo-dir> -b <branch-name>
|
新建一个分支,分支内容就是上面 git bundle create 命令导出的内容
详细展示一行中的修改
清除gitignore文件中记录的文件
展示忽略的文件
commit
历史中显示 Branch1 有的但是 Branch2 没有 commit
1
| $ git log Branch1 ^Branch2
|
在 commit log 中显示 GPG
签名
1
| $ git log --show-signature
|
新建并切换到新分支上,同时这个分支没有任何
commit
1
| $ git checkout --orphan <branch-name>
|
相当于保存修改,但是重写 commit 历史
展示任意分支某一文件的内容
1
| $ git show <branch-name>:<file-name>
|
配置 http 和 socks 代理
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
|
clone 最新一次提交
1
| $ git clone --depth=1 https://github.com/user/repo.git
|
只会 clone
最近一次提交,将减少 clone
时间
忽略某个文件的改动
关闭 track 指定文件的改动,也就是 Git 将不会在记录这个文件的改动
1
| git update-index --assume-unchanged path/to/file
|
恢复 track 指定文件的改动
1
| git update-index --no-assume-unchanged path/to/file
|
以最后提交的顺序列出所有 Git
分支
1
| git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads
|
最新的放在最上面
在 commit log 中查找相关内容
1
| git log --all --grep='<given-text>'
|
通过 grep 查找,given-text: 所需要查找的字段
把暂存区的指定 file
放到工作区中
不添加参数,默认是 -mixed
配置 SSH 协议代理
1 2 3 4
|
Host github.com ProxyCommand nc -X 5 -x 127.0.0.1:1080 %h %p
|
git 代码统计
查看 git 上的个人代码量
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 }' -
|
统计每个人增删行数
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
|
查看仓库提交者排名
这里是排名前十,也可以更改排名
1
| git log --pretty='%aN' | sort | uniq -c | sort -k1 -n -r | head -n 10
|
提交数统计
1
| git log --oneline | wc -l
|