peng@host:~/gitTest$ git log
commit a92aea92169587086679cd13af4cf2dc335ceca5 (HEAD -> master)
Author: songpeng22
Date: Wed Jul 22 21:43:34 2020 +0800
3st commit
commit 0c971470adcbc429c7b2709af18318f2d523439a
Author: songpeng22
Date: Wed Jul 22 21:43:13 2020 +0800
2st commit
commit 3db8de23e1253c48caadf9fb0393659dadf0fdf9
Author: songpeng22
Date: Wed Jul 22 21:42:51 2020 +0800
1st commit
commit 5bb1c9bdcc49ba3238584a6bb45e95aab70a8132 (origin/master, origin/HEAD)
Author: songpeng22
Date: Wed Jul 22 20:40:00 2020 +0800
cmake:hello world
commit a4c972b003daa3e1e854ce3c762237262edb3131
Author: songpeng22 <[email protected]>
Date: Wed Jul 22 19:41:08 2020 +0800
Initial commit
合并最近三次到提交:
git rebase -i HEAD~3
于是出现了(这里是自动用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 = use commit
9 # r, reword = use commit, but edit the commit message
10 # e, edit = use commit, but stop for amending
11 # s, squash = use commit, but meld into previous commit
12 # f, fixup = like "squash", but discard this commit's log message
13 # x, exec = run command (the rest of the line) using shell
14 # b, break = stop here (continue rebase later with 'git rebase --continue')
15 # d, drop = remove commit
16 # l, label
我们想把三次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
略...................................
上面修改后,输入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 # 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
输入git log,可以看到日志合并结果如下:
peng@host:~/gitTest$ git log
commit ee32c58cc8975b40cb7f86abe7c78206b2590256 (HEAD -> master)
Author: songpeng22
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
Date: Wed Jul 22 20:40:00 2020 +0800
cmake:hello world
commit a4c972b003daa3e1e854ce3c762237262edb3131
Author: songpeng22 <[email protected]>
Date: Wed Jul 22 19:41:08 2020 +0800
Initial commit
sudo git push -f origin
如果你的同事在你rebase之前,下载了你想要rebase的commit ,那么你的rebase + force push 可能会带来一些问题。
解决思路1:
可能需要 rebase 的 commit,先不要上传,rebase之后再统一上传。
解决思路2:
各人各用一个branch ,互相不干扰。你上传的时候只 force push 到你自己的branch 上面,如下:
sudo git push -f origin
error: failed to push some refs to 'https://[email protected]/***/***.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第一阶段:入门操作(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