Git合并已经push的commit

场景:我commit了afile,然后commit bfile,并且push到远程仓库了,其实它们属于同一个feature,这时想合并它们。

$ echo "abcd" >> afile.txt

$ git add afile.txt

$ git commit -m "add afile"

$ git push

$ echo "abcd" >> bfile.txt

$ git add bfile.txt

$ git commit -m "add bfile"

$ git push

Git合并已经push的commit_第1张图片

git rebase前两个commit

$ git rebase -i HEAD~2

然后会打开一个文件,键盘按i键进入编辑模式 

Git合并已经push的commit_第2张图片

pick要保留的commit和squash选择这个commi。这里我们pick afile的commit,squash bfile的commit

Git合并已经push的commit_第3张图片

键盘按Esc

键盘按:wq回车保存退出

这时会跳转到commit message编辑界面

Git合并已经push的commit_第4张图片

键盘按i键进入编辑模式,删除squash的那条commit message, 修改pick的那条commit message,键盘按Esc,然后按:wq,回车

Git合并已经push的commit_第5张图片

这时git log可以看到commit被合并了。

bin9w@LAPTOP-ODF35RAF MINGW64 /d/sandbox/code/test-git (main)
$ git log
commit 21d3ab5a1cd37cc1c14f5c6c463b1c58bedd4492 (HEAD -> main)
Author: bin9wei 
Date:   Fri Dec 9 15:41:46 2022 +0800

    add afile and bfile

commit f596f8ad426416b6ae55cb07db2e11d123da33cd
Author: bin9wei 
Date:   Fri Dec 9 15:39:24 2022 +0800

    first commit

bin9w@LAPTOP-ODF35RAF MINGW64 /d/sandbox/code/test-git (main)
$ git push -f
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 288 bytes | 288.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:bin9wei/test-git.git
 + 0fe5528...21d3ab5 main -> main (forced update)

git push -f 强推到远程仓库,这时在github可以看到add afile和add bfile的commit已经合并! 

Git合并已经push的commit_第6张图片

你可能感兴趣的:(Devops,git)