本文主要是介绍Git第六阶段:Rebase -> 合并多个commit为一个,使提交更为简洁。,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.如下所示,自上次push以后,我在本地有三次零散的commit,但是还没有push,因为不想提交到server上显得太过凌乱,所以要合并以下再push,这就要用到’git rebase’:
peng@host:~/gitTest$ git log
commit a92aea92169587086679cd13af4cf2dc335ceca5 (HEAD -> master)
Author: songpeng22 <songpeng24@msn.com>
Date: Wed Jul 22 21:43:34 2020 +08003st commit
commit 0c971470adcbc429c7b2709af18318f2d523439a
Author: songpeng22 <songpeng24@msn.com>
Date: Wed Jul 22 21:43:13 2020 +08002st commit
commit 3db8de23e1253c48caadf9fb0393659dadf0fdf9
Author: songpeng22 <songpeng24@msn.com>
Date: Wed Jul 22 21:42:51 2020 +08001st commit
commit 5bb1c9bdcc49ba3238584a6bb45e95aab70a8132 (origin/master, origin/HEAD)
Author: songpeng22 <songpeng24@msn.com>
Date: Wed Jul 22 20:40:00 2020 +0800cmake:hello world
commit a4c972b003daa3e1e854ce3c762237262edb3131
Author: songpeng22 <31085468+songpeng22@users.noreply.github.com>
Date: Wed Jul 22 19:41:08 2020 +0800Initial commit
2.git rebase的使用:
合并最近三次到提交:
git rebase -i HEAD~3
于是出现了(这里是自动用vim打开):
1 pick 3db8de2 1st commit2 pick 0c97147 2st commit3 pick a92aea9 3st commit4 5 # Rebase 5bb1c9b..a92aea9 onto 5bb1c9b (3 commands)6 #7 # Commands:8 # p, pick <commit> = use commit9 # r, reword <commit> = use commit, but edit the commit message10 # e, edit <commit> = use commit, but stop for amending11 # s, squash <commit> = use commit, but meld into previous commit12 # f, fixup <commit> = like "squash", but discard this commit's log message13 # x, exec <command> = run command (the rest of the line) using shell14 # b, break = stop here (continue rebase later with 'git rebase --continue')15 # d, drop <commit> = remove commit16 # l, label <label> = label current HEAD with a name17 # t, reset <label> = reset HEAD to a label18 # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]19 # . create a merge commit using the original merge commit's20 # . message (or the oneline, if no original merge commit was21 # . specified). Use -c <commit> to reword the commit message.22 #23 # These lines can be re-ordered; they are executed from top to bottom.24 #25 # If you remove a line here THAT COMMIT WILL BE LOST.26 #27 # However, if you remove everything, the rebase will be aborted.28 #29 # Note that empty commits are commented out
3. 合并方式
我们想把三次commit 合并成一个,这就要用到上面文本里到一些操作命令。这里选squash( use commit , but meld into previous commit) ,将后面的塞入之前的commit
将上面前三行的内容修改为
1 p 3db8de2 1st commit2 s 0c97147 2st commit3 s a92aea9 3st commit
略...................................
4.修改commit日志
上面修改后,输入wq保存后,跳出修改日志的内容:
1 # This is a combination of 3 commits.2 # This is the 1st commit message:4 1st commit6 # This is the commit message #2:8 2st commit10 # This is the commit message #3:12 3st commit14 # Please enter the commit message for your changes. Lines starting15 # with '#' will be ignored, and an empty message aborts the commit.16 #17 # Date: Wed Jul 22 21:42:51 2020 +080018 #19 # interactive rebase in progress; onto 5bb1c9b20 # Last commands done (3 commands done):21 # squash 0c97147 2st commit22 # squash a92aea9 3st commit23 # No commands remaining.24 # You are currently rebasing branch 'master' on '5bb1c9b'.26 # Changes to be committed:27 # modified: README.md28 #
我将之修改为:
1 # This is a combination of 3 commits.2 combined log:3 1st commit4 2st commit5 3st commit7 # Please enter the commit message for your changes. Lines starting8 # with '#' will be ignored, and an empty message aborts the commit.9 #10 # Date: Wed Jul 22 21:42:51 2020 +080011 #12 # interactive rebase in progress; onto 5bb1c9b13 # Last commands done (3 commands done):14 # squash 0c97147 2st commit15 # squash a92aea9 3st commit16 # No commands remaining.17 # You are currently rebasing branch 'master' on '5bb1c9b'.18 #19 # Changes to be committed:20 # modified: README.md
5.结果
输入git log,可以看到日志合并结果如下:
peng@host:~/gitTest$ git log
commit ee32c58cc8975b40cb7f86abe7c78206b2590256 (HEAD -> master)
Author: songpeng22 <songpeng24@msn.com>
Date: Wed Jul 22 21:42:51 2020 +0800combined log:1st commit2st commit3st commit
commit 5bb1c9bdcc49ba3238584a6bb45e95aab70a8132 (origin/master, origin/HEAD)
Author: songpeng22 <songpeng24@msn.com>
Date: Wed Jul 22 20:40:00 2020 +0800cmake:hello world
commit a4c972b003daa3e1e854ce3c762237262edb3131
Author: songpeng22 <31085468+songpeng22@users.noreply.github.com>
Date: Wed Jul 22 19:41:08 2020 +0800Initial commit
6.push
sudo git push -f origin <branch>
6-1. rebase + force push 可能会导致的混乱
如果你的同事在你rebase之前,下载了你想要rebase的commit ,那么你的rebase + force push 可能会带来一些问题。
解决思路1:
可能需要 rebase 的 commit,先不要上传,rebase之后再统一上传。
解决思路2:
各人各用一个branch ,互相不干扰。你上传的时候只 force push 到你自己的branch 上面,如下:
sudo git push -f origin <branch>
6-2 错误做法 - 如果直接 git push ,会被reject , 有类似提示:
error: failed to push some refs to 'https://name@github.com/***/***.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
原因是rebase之后,remote repository 的 commit 和 local repository的commit 就不一样了,让你merge。
比较奇怪的是 为什么说 current branch 是 behind remote repository。
我的理解,也许以服务器为起源/origin的话,只要服务器有你没有的commit , 你就算behind了。
参考文献:
彻底搞懂git:
http://jartto.wang/2018/12/11/git-rebase/
Git commits历史是如何做到如此清爽的:
https://www.zhihu.com/question/61283395
这篇关于Git第六阶段:Rebase -> 合并多个commit为一个,使提交更为简洁。的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!