git rebase -i 的两个使用
- 修改历史提交
- 使用 git rebase -i 合并提交
修改历史提交
先看下当前状态:
1 2 3 4 5 6 7 8 9 10 11 | $ cat file.txt 00000000000 111111111 222222222 333333333 $ git log --oneline b385975 (HEAD -> master) 33333333 1eedfbc 22222222 675bd1d 111111111 16ba3a2 0000000 |
文本中四行代码分别对应四个提交。
我们现在想修改
1 2 3 4 5 6 7 8 9 | $ 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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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 |
此时代码已经回到我们提交
1 2 3 4 5 6 7 8 9 10 11 12 | $ 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 |
修改
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | $ 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 $ |
最终,我们可以看到
使用 git rebase -i 合并提交
现在我们感觉
1 2 3 4 5 6 7 8 9 10 11 | $ 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. |
将
1 2 3 | 675bd1d 111111111 s 1eedfbc 22222222.2222 # 将此处的pick改为s s b385975 33333333 # 将此处的pick改为s |
现在我们看下最后状态:
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 | $ 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 <[email protected]> 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 $ |
打完收工。