git rebase -i 的两个使用
修改历史提交
先看下当前状态:
$ cat file.txt
00000000000
111111111
222222222
333333333
$ git log --oneline
b385975 (HEAD -> master) 33333333
1eedfbc 22222222
675bd1d 111111111
16ba3a2 0000000
文本中四行代码分别对应四个提交。
我们现在想修改 1eedfbc 22222222
这行提交。
$ git rebase -i 675bd1d
Stopped at 1eedfbc... 22222222
You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continue
e 1eedfbc 22222222 # 将此处的pick改为edit
pick b385975 33333333
# Rebase 675bd1d..b385975 onto 675bd1d (2 commands)
#
# 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
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
此时代码已经回到我们提交22222
的时候。
$ git status
interactive rebase in progress; onto 675bd1d
Last command done (1 command done):
edit 1eedfbc 22222222
Next command to do (1 remaining command):
pick b385975 33333333
(use "git rebase --edit-todo" to view and edit)
You are currently editing a commit while rebasing branch 'master' on '675bd1d'.
(use "git commit --amend" to amend the current commit)
(use "git rebase --continue" once you are satisfied with your changes)
nothing to commit, working tree clean
修改file.txt
并提交
$ cat file.txt
00000000000
111111111
222222222.2222
$ git status
interactive rebase in progress; onto 675bd1d
Last command done (1 command done):
edit 1eedfbc 22222222
Next command to do (1 remaining command):
pick b385975 33333333
(use "git rebase --edit-todo" to view and edit)
You are currently editing a commit while rebasing branch 'master' on '675bd1d'.
(use "git commit --amend" to amend the current commit)
(use "git rebase --continue" once you are satisfied with your changes)
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: file.txt
no changes added to commit (use "git add" and/or "git commit -a")
$
$ git add .
$ git commit --amend
[detached HEAD f6681cb] 22222222.2222
Date: Fri Jun 5 16:11:06 2020 +0800
1 file changed, 1 insertion(+)
git commit --amend
[detached HEAD f6681cb] 22222222.2222
Date: Fri Jun 5 16:11:06 2020 +0800
1 file changed, 1 insertion(+)
$
$ git rebase --continue
Auto-merging file.txt
CONFLICT (content): Merge conflict in file.txt
error: could not apply b385975... 33333333
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply b385975... 33333333
$ git status
interactive rebase in progress; onto 675bd1d
Last commands done (2 commands done):
edit 1eedfbc 22222222
pick b385975 33333333
No commands remaining.
You are currently rebasing branch 'master' on '675bd1d'.
(fix conflicts and then run "git rebase --continue")
(use "git rebase --skip" to skip this patch)
(use "git rebase --abort" to check out the original branch)
Unmerged paths:
(use "git reset HEAD <file>..." to unstage)
(use "git add <file>..." to mark resolution)
both modified: file.txt
no changes added to commit (use "git add" and/or "git commit -a")
$
$ vim file.txt
$
$ git add .
$
$ git rebase --continue
[detached HEAD e1e4d5f] 33333333
1 file changed, 1 insertion(+)
Successfully rebased and updated refs/heads/master.
$
$
$
$ git log --oneline
e1e4d5f (HEAD -> master) 33333333
f6681cb 22222222.2222
675bd1d 111111111
16ba3a2 0000000
$ cat file.txt
00000000000
111111111
222222222.2222
333333333
$
最终,我们可以看到22222222
这行提交的内容和commit信息都成功被修改。
使用 git rebase -i 合并提交
现在我们感觉222222222.2222
和 333333333
这两个提交没用,想把它合并到111111111
里面,我们来看下操作流程。
$ git log --oneline
e1e4d5f (HEAD -> master) 33333333
f6681cb 22222222.2222
675bd1d 111111111
16ba3a2 0000000
$
$ git rebase -i 16ba3a2
[detached HEAD d8a165d] 111111111
Date: Fri Jun 5 16:10:12 2020 +0800
1 file changed, 3 insertions(+)
Successfully rebased and updated refs/heads/master.
将22222222.2222
和33333333
前面的lable改为s。
675bd1d 111111111
s 1eedfbc 22222222.2222 # 将此处的pick改为s
s b385975 33333333 # 将此处的pick改为s
现在我们看下最后状态:
$ git log --oneline
d8a165d (HEAD -> master) 111111111
16ba3a2 0000000
$
$ cat file.txt
00000000000
111111111
222222222.2222
333333333
$ git show d8a165d
commit d8a165d480cd92fffe09e4b03c53bfaa22021275 (HEAD -> master)
Author: Lei Guo <guolei@hygon.cn>
Date: Fri Jun 5 16:10:12 2020 +0800
111111111
22222222.2222
33333333
diff --git a/file.txt b/file.txt
index e1cef00..bc85d51 100644
--- a/file.txt
+++ b/file.txt
@@ -1 +1,4 @@
00000000000
+111111111
+222222222.2222
+333333333
$
打完收工。