供日常工作查阅参考
查看单个文件提交修改记录
1
2
git log -p <文件名> # 输出 commit信息+文件改动信息
git log -- -p <文件名> # 仅输出commit记录
使用上一个commit信息并编辑或修改上一次commit信息
1
git commit --amend
撤销某一次commit及文件改动,并生成新的commit信息
1
2
3
git revert <commit>
# 也可以指定范围内的commit 格式为 <commit>...<commit>
# (x...y] 包左不包右
修改最近N次提交记录(删除,合并)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
git rebase -i HEAD~<N>
# 命令:
# p, pick <提交> = 使用提交
# r, reword <提交> = 使用提交,但修改提交说明
# e, edit <提交> = 使用提交,进入 shell 以便进行提交修补
# s, squash <提交> = 使用提交,但融合到前一个提交
# f, fixup <提交> = 类似于 "squash",但丢弃提交说明日志
# x, exec <命令> = 使用 shell 运行命令(此行剩余部分)
# b, break = 在此处停止(使用 'git rebase --continue' 继续变基)
# d, drop <提交> = 删除提交
# l, label <label> = 为当前 HEAD 打上标记
# t, reset <label> = 重置 HEAD 到该标记
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . 创建一个合并提交,并使用原始的合并提交说明(如果没有指定
# . 原始提交,使用注释部分的 oneline 作为提交说明)。使用
# . -c <提交> 可以编辑提交说明。
添加关联远程仓库
1
git remote add <仓库名> <仓库地址>
展示当前项目关联的所欲远程仓库名
1
git remote -v
查看远程仓库所有分支
1
git branch -a
远程仓库同步已删除的分支(清理无效的远程追踪的分支)
1
2
# git remote show <仓库名> 查看remote地址,远程分支,还有本地分支与之相对应关系等信息
git remote prune <仓库名>
撤销commit
1
2
3
git reset --soft HEAD^ # 撤销commit,代码保留
git reset --mixed HEAD^ # 同时撤销add
git reset --hard HEAD^ # 同时删除所有改动
撤销rebase
1
2
git reflog # 查看最近操作记录
git reset --hard commitID #撤销
储藏
1
2
3
4
5
6
7
8
9
git stash list # 展示当前的存储列表
git stash # 贮藏当前改动
git stash save 'xxx' # 贮藏当前改动并命名
git stash pop # 删除并应用最近一次贮藏 stash@{0}
git stash apply # 应用最近一次贮藏 stash@{0}
git stash drop # 删除最近一次贮藏 stash@{0}
git stash clear # 清空贮藏
git stash show # 显示最近一次储藏的 文件改动(只显示文件信息)
git stash show -p # 显示最近一次贮藏的 具体改动(显示所有改动信息)
cherry-pick
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
git cherry-pick [<选项>] <提交号>... 或:git cherry-pick <子命令>
--quit 终止反转或拣选操作
--continue 继续反转或拣选操作
--abort 取消反转或拣选操作
-n, --no-commit 不要自动提交
-e, --edit 编辑提交说明
-s, --signoff 添加 Signed-off-by: 签名
-m, --mainline <父编号>
选择主干父提交编号
--rerere-autoupdate 如果可能,重用冲突解决更新索引
--strategy <策略> 合并策略
-X, --strategy-option <选项>
合并策略的选项
-S, --gpg-sign[=<key-id>]
GPG 提交签名
-x 追加提交名称
--ff 允许快进式
--allow-empty 保留初始化的空提交
--allow-empty-message
允许提交说明为空
--keep-redundant-commits
保持多余的、空的提交
git cherry-pick <commit id>:单独合并一个提交
git cherry-pick -x <commit id>:同上,不同点:保留原提交者信息。
Git从1.7.2版本开始支持批量cherry-pick,就是一次可以cherry-pick一个区间的commit。
git cherry-pick <start-commit-id>..<end-commit-id>
git cherry-pick <start-commit-id>^..<end-commit-id>
# 前者表示把<start-commit-id>到<end-commit-id>之间(左开右闭,不包含start-commit-id)的提交cherry-pick到当前分支;后者有"^"标志的表示把<start-commit-id>到<end-commit-id>之间(闭区间,包含start-commit-id)的提交cherry-pick到当前分支。其中,<start-commit-id>到<end-commit-id>只需要commit-id的前6位即可,并且<start-commit-id>在时间上必须早于<end-commit-id>
git 修改 commit 作者信息
1
2
3
4
5
6
7
8
9
10
11
12
13
# 指定当前提交 作者信息
git commit -m "Initial commit" --author="Yijio<123456@qq.com>"
# 修改上次commit作者信息
git commit --amend --author="name<email>"
# 修改前N次的commit作者信息
# 1. git rebase -i HEAD~n
# 2. pick 修改为 edit 或者 e
# 3. git commit -m "Initial commit" --author="name<email>"
# 4. git rebase continue
# 修改所有提交 (未验证)
git rebase -i --root
项目.gitignore 文件新增文件不生效
1
2
3
# 要清除git对所有文件的trace:
git rm -r --cached .
然后重新 commit
清除登录信息
1
git config --system --unset credential.helper