当前位置:   article > 正文

Git第六阶段:Rebase -> 合并多个commit为一个,使提交更为简洁。_rebase onto remote和drop local

rebase onto remote和drop local
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 +0800
    3st commit
commit 0c971470adcbc429c7b2709af18318f2d523439a
Author: songpeng22 <songpeng24@msn.com>
Date:   Wed Jul 22 21:43:13 2020 +0800
    2st commit
commit 3db8de23e1253c48caadf9fb0393659dadf0fdf9
Author: songpeng22 <songpeng24@msn.com>
Date:   Wed Jul 22 21:42:51 2020 +0800
    1st commit
commit 5bb1c9bdcc49ba3238584a6bb45e95aab70a8132 (origin/master, origin/HEAD)
Author: songpeng22 <songpeng24@msn.com>
Date:   Wed Jul 22 20:40:00 2020 +0800
    cmake:hello world
commit a4c972b003daa3e1e854ce3c762237262edb3131
Author: songpeng22 <31085468+songpeng22@users.noreply.github.com>
Date:   Wed Jul 22 19:41:08 2020 +0800
    Initial commit
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
2.git rebase的使用:

合并最近三次到提交:

git rebase -i HEAD~3
  • 1

于是出现了(这里是自动用vim打开):

  1 pick 3db8de2 1st commit
  2 pick 0c97147 2st commit
  3 pick a92aea9 3st commit
  4 
  5 # Rebase 5bb1c9b..a92aea9 onto 5bb1c9b (3 commands)
  6 #
  7 # Commands:
  8 # p, pick <commit> = use commit
  9 # r, reword <commit> = use commit, but edit the commit message
 10 # e, edit <commit> = use commit, but stop for amending
 11 # s, squash <commit> = use commit, but meld into previous commit
 12 # f, fixup <commit> = like "squash", but discard this commit's log message
 13 # x, exec <command> = run command (the rest of the line) using shell
 14 # b, break = stop here (continue rebase later with 'git rebase --continue')
 15 # d, drop <commit> = remove commit
 16 # l, label <label> = label current HEAD with a name
 17 # t, reset <label> = reset HEAD to a label
 18 # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
 19 # .       create a  merge commit using the original merge commit's
 20 # .       message (or the oneline, if no original merge commit was
 21 # .       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
  • 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
3. 合并方式

我们想把三次commit 合并成一个,这就要用到上面文本里到一些操作命令。这里选squash( use commit , but meld into previous commit) ,将后面的塞入之前的commit
将上面前三行的内容修改为

  1 p 3db8de2 1st commit
  2 s 0c97147 2st commit
  3 s a92aea9 3st commit
略...................................

  • 1
  • 2
  • 3
  • 4
  • 5
4.修改commit日志

上面修改后,输入wq保存后,跳出修改日志的内容:

  1 # This is a combination of 3 commits.
  2 # This is the 1st commit message:
  4 1st commit
  6 # This is the commit message #2:
  8 2st commit
 10 # This is the commit message #3:
 12 3st commit
 14 # Please enter the commit message for your changes. Lines starting
 15 # with '#' will be ignored, and an empty message aborts the commit.
 16 #
 17 # Date:      Wed Jul 22 21:42:51 2020 +0800
 18 #
 19 # interactive rebase in progress; onto 5bb1c9b
 20 # Last commands done (3 commands done):
 21 #    squash 0c97147 2st commit
 22 #    squash a92aea9 3st commit
 23 # No commands remaining.
 24 # You are currently rebasing branch 'master' on '5bb1c9b'.
 26 # Changes to be committed:
 27 #   modified:   README.md
 28 #
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

我将之修改为

  1 # This is a combination of 3 commits.
  2 combined log:
  3 1st commit
  4 2st commit
  5 3st commit
  7 # Please enter the commit message for your changes. Lines starting
  8 # with '#' will be ignored, and an empty message aborts the commit.
  9 #
 10 # Date:      Wed Jul 22 21:42:51 2020 +0800
 11 #
 12 # interactive rebase in progress; onto 5bb1c9b
 13 # Last commands done (3 commands done):
 14 #    squash 0c97147 2st commit
 15 #    squash a92aea9 3st commit
 16 # No commands remaining.
 17 # You are currently rebasing branch 'master' on '5bb1c9b'.
 18 #
 19 # Changes to be committed:
 20 #   modified:   README.md
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
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 +0800
    combined log:
    1st commit
    2st commit
    3st commit
commit 5bb1c9bdcc49ba3238584a6bb45e95aab70a8132 (origin/master, origin/HEAD)
Author: songpeng22 <songpeng24@msn.com>
Date:   Wed Jul 22 20:40:00 2020 +0800
    cmake:hello world
commit a4c972b003daa3e1e854ce3c762237262edb3131
Author: songpeng22 <31085468+songpeng22@users.noreply.github.com>
Date:   Wed Jul 22 19:41:08 2020 +0800
    Initial commit
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
6.push
sudo git push -f origin <branch>
  • 1
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.
  • 1
  • 2
  • 3
  • 4
  • 5

原因是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第一阶段:入门操作(check in riles/checkout files)
https://www.cppblog.com/hkingSP/archive/2019/03/02/216277.html
Git第二阶段:add files/undo add/branch add/branch view/branch switch/branch delete/pull remote to local files/fetch origin
https://www.cppblog.com/hkingSP/archive/2019/03/04/216286.html
Git第三阶段:show log/show head/show modified/modify comment
https://www.cppblog.com/hkingSP/archive/2019/03/05/216287.html
Git第四阶段:big project clone / 速度太慢
https://blog.csdn.net/songpeng26/article/details/104999088
Git第五阶段:git patch
https://blog.csdn.net/songpeng26/article/details/105016398
Git第六阶段:Rebase -> 合并多个commit为一个,使提交更为简洁
https://blog.csdn.net/songpeng26/article/details/107523879
Git第七阶段:http/https proxy/ssh proxy/git proxy
https://blog.csdn.net/songpeng26/article/details/108078573
Git第八阶段:git tag and git describe a specified path/commits/tags - 231211
https://www.cnblogs.com/hkingsp/p/17893783.html
Git第九阶段:git blame - find last modified by whom
https://www.cnblogs.com/hkingsp/p/14981097.html
Git第十阶段:git submodule list/clone/update
https://www.cnblogs.com/hkingsp/p/15128126.html
Git第十一阶段:git status | git clean -fd ,查看某个文件夹的状态,清除某个文件夹的untracked files
https://www.cnblogs.com/hkingsp/p/16329894.html
Git第十二阶段:git stash用于迁移
https://www.cnblogs.com/hkingsp/p/17845609.html
Git第十三阶段:Rebase -> 调整commit 顺序
https://www.cnblogs.com/hkingsp/p/18024489

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号