将已经 push 到远端的两个 commit 合并成一个 commit 应该怎么做【热度: 103】

关键词:git rebase 合并 commit

当你已经将两个 commit 推送到远端仓库,现在想要将它们合并成一个 commit,可以按照以下步骤操作:

1. 克隆仓库到本地

如果你还没有在本地克隆该仓库,需要先将远程仓库克隆到本地:

git clone <远程仓库地址>
cd <仓库目录>

2. 查看提交历史

使用 git log 命令查看提交历史,确认要合并的两个 commit 的哈希值(commit hash)。一般来说,新的 commit 在前面,旧的 commit 在后面。

git log --oneline

例如,输出可能如下:

abcdefg 第三个提交
1234567 第二个提交
890abcd 第一个提交

假设你要合并的是 1234567abcdefg 这两个 commit

3. 进行交互式变基

使用 git rebase -i 命令进行交互式变基,指定要合并的 commit 的前一个 commit 的哈希值。在这个例子中,就是 890abcd

git rebase -i 890abcd

执行上述命令后,会打开一个文本编辑器,显示如下内容:

pick abcdefg 第三个提交
pick 1234567 第二个提交

# Rebase 890abcd..abcdefg onto 890abcd (2 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

4. 修改提交操作

将第二个 commit 前面的 pick 改为 squashs,表示将这个 commit 合并到前一个 commit 中。修改后的内容如下:

pick abcdefg 第三个提交
s 1234567 第二个提交

保存并关闭文本编辑器。

5. 修改合并后的提交信息

保存退出后,会再次打开一个文本编辑器,让你修改合并后的 commit 信息。你可以保留原有的 commit 信息,也可以根据需要进行修改。修改完成后,保存并关闭文本编辑器。

6. 强制推送修改到远程仓库

由于你已经修改了提交历史,本地的提交历史和远程仓库的提交历史不再一致,需要使用 git push -f 命令强制推送修改到远程仓库:

git push -f origin <分支名>

注意:强制推送会覆盖远程仓库的提交历史,可能会影响其他团队成员的工作,建议在操作前与团队成员沟通。

7. 通知团队成员更新本地仓库

强制推送后,通知团队成员拉取最新的提交历史:

git pull --rebase origin <分支名>

通过以上步骤,你就可以将已经推送到远端的两个 commit 合并成一个 commit