当前位置:   article > 正文

Git——rebase命令_last commands done

last commands done

1. 应用场景

  • 合并多次提交记录
  • 分支合并
  • 对一个分支做『变基』操作

2. 合并多次提交记录

合并最近两次commit:

git rebase -i HEAD~2

执行命令后会自动进入 vi 编辑模式:

  1. pick 6935383 rename冲突
  2. pick 23b1a51 debug
  3. # Rebase eb7f366..23b1a51 onto eb7f366 (2 commands)
  4. #
  5. # Commands:
  6. # p, pick <commit> = use commit
  7. # r, reword <commit> = use commit, but edit the commit message
  8. # e, edit <commit> = use commit, but stop for amending
  9. # s, squash <commit> = use commit, but meld into previous commit
  10. # f, fixup <commit> = like "squash", but discard this commit's log message
  11. # x, exec <command> = run command (the rest of the line) using shell
  12. # b, break = stop here (continue rebase later with 'git rebase --continue')
  13. # d, drop <commit> = remove commit
  14. # l, label <label> = label current HEAD with a name
  15. # t, reset <label> = reset HEAD to a label
  16. # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
  17. # . create a merge commit using the original merge commit's
  18. # . message (or the oneline, if no original merge commit was
  19. # . specified). Use -c <commit> to reword the commit message.
  20. #
  21. # These lines can be re-ordered; they are executed from top to bottom.
  22. #
  23. # If you remove a line here THAT COMMIT WILL BE LOST.
  24. #
  25. # However, if you remove everything, the rebase will be aborted.

【注】

  • 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

修改提交纪录并保存退出。

  1. pick 6935383 rename冲突
  2. s 23b1a51 debug

保存后到注释修改界面。

  1. # This is a combination of 2 commits.
  2. # This is the 1st commit message:
  3. rename冲突
  4. # This is the commit message #2:
  5. debug
  6. # Please enter the commit message for your changes. Lines starting
  7. # with '#' will be ignored, and an empty message aborts the commit.
  8. #
  9. # Date: Tue Nov 24 22:56:02 2020 +0800
  10. #
  11. # interactive rebase in progress; onto eb7f366
  12. # Last commands done (2 commands done):
  13. # pick 6935383 rename冲突
  14. # squash 23b1a51 debug
  15. # No commands remaining.
  16. # You are currently rebasing branch 'maseter' on 'eb7f366'.

(非编辑状态输入dd删除一行)

保存后即合并完成。

异常情况:

1、不要合并已提交远程分支的纪录

如果这样做,可能出现push rejected。 解决方式当然是先拉下远程的代码,进行冲突处理,再进行提交。

2、git rebase -i 异常操作导致退出了vim编辑页面,会提示:

  1. $ git rebase -i head~2
  2. error: could not apply 040bd4b... commit on issue-005
  3. Resolve all conflicts manually, mark them as resolved with
  4. "git add/rm <conflicted_files>", then run "git rebase --continue".
  5. You can instead skip this commit: run "git rebase --skip".
  6. To abort and get back to the state before "git rebase", run "git rebase --abort".
  7. Could not apply 040bd4b... commit on issue-005
  8. Auto-merging README.md
  9. CONFLICT (content): Merge conflict in README.md

使用 git rebase --edit-todo 会再次进入刚才编辑错误退出前的vim状态,这时候可以修改你的编辑。

使用git rebase --abort  表明退出当前的合并请求( 又回到原来的2个commit的状态)

3. 变基

使用场景:本地与远端同一分支提交历史不一致

多个人在同一个分支上协作时,比如由我和A一同开发。我在修复了一个bug以后准备提交。

  1. Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (master)
  2. $ git add .
  3. Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (master)
  4. $ git commit -m "debug the world"
  5. [master a40d43c] debug the world
  6. 1 file changed, 0 insertions(+), 0 deletions(-)
  7. create mode 100644 aa.txt
  8. Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (master)
  9. $ git push origin master
  10. To github.com:Zhangtao153/learngit.git
  11. ! [rejected] master -> master (non-fast-forward)
  12. error: failed to push some refs to 'github.com:Zhangtao153/learngit.git'
  13. hint: Updates were rejected because the tip of your current branch is behind
  14. hint: its remote counterpart. Integrate the remote changes (e.g.
  15. hint: 'git pull ...') before pushing again.
  16. hint: See the 'Note about fast-forwards' in 'git push --help' for details.

push失败了,说明A在我之前已经提交了,我本地master分支的提交历史已经落后远端了,需要先pull一下,与远端同步后才能push

  1. Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (master)
  2. $ git pull
  3. Merge made by the 'recursive' strategy.
  4. README.md | 3 ++-
  5. 1 file changed, 2 insertions(+), 1 deletion(-)
  6. Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (master)
  7. $ git log --oneline --graph
  8. * 912bb85 (HEAD -> master) Merge branch 'master' of github.com:Zhangtao153/learngit into master
  9. |\
  10. | * 7ad4fd6 (origin/master) add a new function
  11. * | a40d43c debug the world
  12. |/
  13. * a8d2e0d 手动删除
  14. * 1d243ca add test file
  15. * 8438389 wrote a readme file

竟然分叉了!由于我本地master的提交历史和远端的master分支的提交历史不一致,所以git为我进行了自动合并,然后生成了一个新的提交历史。

这个时候就可以用 git rebase 解决分叉的问题。

  1. $ git rebase
  2. Successfully rebased and updated refs/heads/master.
  3. Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (master)
  4. $ git log --oneline --graph
  5. * 4178b6e (HEAD -> master) debug the world
  6. * 7ad4fd6 (origin/master) add a new function
  7. * a8d2e0d 手动删除
  8. * 1d243ca add test file
  9. * 8438389 wrote a readme file

然后再push,将本地修改同步到远端。

git pull --rebase 和上面的效果一致。

4. 合并分支

先创建一个分支用于解决bug

  1. $ git checkout -b issues-001
  2. Switched to a new branch 'issues-001'

接下解决bug,然后保存提交

  1. Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (issues-001)
  2. $ git add .
  3. Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (issues-001)
  4. $ git commit -m "issues-001"
  5. [issues-001 7a55a26] issues-001
  6. 1 file changed, 1 insertion(+)

先尝试通过 merge 合并:

  1. Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (issues-001)
  2. $ git switch master
  3. Switched to branch 'master'
  4. Your branch is ahead of 'origin/master' by 1 commit.
  5. (use "git push" to publish your local commits)
  6. Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (master)
  7. $ git merge issues-001
  8. Merge made by the 'recursive' strategy.
  9. aa.txt | 1 +
  10. 1 file changed, 1 insertion(+)
  11. Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (master)
  12. $ git log --oneline --graph
  13. * ae02ea0 (HEAD -> master) Merge branch 'issues-001' into master
  14. |\
  15. | * 7a55a26 (issues-001) issues-001
  16. * | d36d166 commit on master
  17. |/
  18. * 4178b6e (origin/master) debug the world
  19. * 7ad4fd6 add a new function
  20. * a8d2e0d 手动删除
  21. * 1d243ca add test file
  22. * 8438389 wrote a readme file

虽然合并成功,但是Master已经保存了合并历史,出现开叉了!

通过rebase合并分支

先将代码回退到merge之前

  1. Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (master)
  2. $ git reset --hard HEAD^
  3. HEAD is now at d36d166 commit on master

先切换回issues-001分支,在issues-001分支上执行: git rebase master

  1. Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (master)
  2. $ git switch issues-001
  3. Switched to branch 'issues-001'
  4. Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (issues-001)
  5. $ git rebase master
  6. Successfully rebased and updated refs/heads/issues-001.

【注】如果rebase出现冲突,先解决冲突,然后通过add添加,之前的rebase其实只是完成了一半,由于出现冲突而终止,冲突解决之后,可以通过git rebase —continue继续完成之前的rebase操作。

切换到主分支master,将issues-001分支上的提交合并过来。

  1. Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (issues-001)
  2. $ git switch master
  3. Switched to branch 'master'
  4. Your branch is ahead of 'origin/master' by 1 commit.
  5. (use "git push" to publish your local commits)
  6. Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (master)
  7. $ git merge issues-001
  8. Updating d36d166..43823d4
  9. Fast-forward
  10. aa.txt | 1 +
  11. 1 file changed, 1 insertion(+)
  12. Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (master)
  13. $ git log --oneline --graph
  14. * 43823d4 (HEAD -> master, issues-001) issues-001
  15. * d36d166 commit on master
  16. * 4178b6e (origin/master) debug the world
  17. * 7ad4fd6 add a new function
  18. * a8d2e0d 手动删除
  19. * 1d243ca add test file
  20. * 8438389 wrote a readme file

master是一条直线了。最后删除掉issues-001分支。

  1. Administrator@WIN-DTNF3GRDH5R MINGW64 /g/learngit (master)
  2. $ git branch -d issues-001
  3. Deleted branch issues-001 (was 43823d4).

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/476569
推荐阅读
相关标签
  

闽ICP备14008679号