折腾git,被git折腾,还是保持够用就好的原则?
https://backlog.com/git-tutorial/cn/stepup/stepup7_1.html
https://git-scm.com/book/zh/v2
http://gitbook.liuhui998.com/index.html
以下情景都属于改写提交
基本都是基于git rebase
命令
回到第一次提交commit 1请使用
git rebase -i --root master
1 . git rebase -i
到commit 2的上一次提交commit 1
git rebase -i 5a25878
# 执行完毕会弹出编辑器
2 . 修改commit 3 pick
命令为squash
, 和commit2 合并(squash命令: use commit, but meld into previous commit/使用提交,但合并进先前的提交)
# 编辑前内容
pick 8a98f84 commit 2, add 2.txt
pick 5d2b9ba commit 3, add 3.txt
pick 0768163 commit 4, add 4.txt
pick 22e45ec commit 5, add 5.txt
# Rebase 5a25878..22e45ec onto 5a25878 (4 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# 编辑后内容
pick 8a98f84 commit 2, add 2.txt
# 修改了改行
squash 5d2b9ba commit 3, add 3.txt
pick 0768163 commit 4, add 4.txt
pick 22e45ec commit 5, add 5.txt
3 . 保存上一步文本内容, 会弹出编辑器提示修改两次合并后的消息内容, 删除 commit 2的消息, commit 3不变, 然后保存
效果如下,仅删除了提交记录log,文件内容保持不变
修改最近一次提交信息
git commit --amend
# 修改保存即可
修改以前的提交信息
回到第一次提交commit 1请使用
git rebase -i --root master
# git rebase -i 要修改的commit的上一级commit id
# 使用reword命令
# 22e45ec commit 5, add 5.txt
# 0768163 commit 4, add 4.txt
# 5d2b9ba commit 3, add 3.txt
# 8a98f84 commit 2, add 2.txt
# 5a25878 commit 1, add 1.txt
# 以修改commit 2为例, 先rebase到commit 2的上一级commit 1
git rebase -i 5a25878
# 修改commit 2的rebase命令pick为reword
# 保存,弹出编辑器提示修改提交信息,修改为要更正的信息保存即可
回到第一次提交commit 1请使用
git rebase -i --root master
无冲突
drop
命令应用到commit 2有冲突
情景
需要删除commit 2提交,但commit 6依赖2.txt, 而 commit 2创建的2.txt
drop
命令应用到commit 2leo@demo:~/tutorial$ git rebase -i 5a25878
error: could not apply cb75f2e... commit 6, 修改了2.txt
When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".
Could not apply cb75f2ecf6ab2d837a61bd95531b306ed22258d8... commit 6, 修改了2.txt
# git rebase 执行" pick cb75f2e commit 6, 修改了2.txt "检测到与"删除commit 2"有冲突,需要手动解决冲突后才能继续进行
git add 冲突的文件
/ git add .
然后git rebase --continue
提交。(本例中冲突处理是, 保留2.txt和commit 6修改的内容,然后提交)git filter-branch -f --commit-filter '
if [ "$GIT_AUTHOR_EMAIL" = "[email protected]" ];
then
GIT_AUTHOR_NAME="leo2";
GIT_AUTHOR_EMAIL="[email protected]";
git commit-tree "$@";
else
git commit-tree "$@";
fi' HEAD
# 可以限定范围commit xx到当前head执行替换操作
git filter-branch -f --commit-filter '....command' 5a258787..HEAD
# 但是如何限定范围commit xx 到中间的某个commit xx而不是HEAD如何实现不清楚
git filter-branch --tree-filter 'rm -f passwords.txt' HEAD