在 Git 中删除合并提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30561895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Removing merge commits in Git
提问by Jelean Thomas
Here, I have our commit log from my repo. You can see the "Merge branch...into..." commits, and after that commit, the branch was merged.
在这里,我有来自我的 repo 的提交日志。你可以看到“Merge branch...into...”提交,在提交之后,分支被合并了。
I need to delete that merge commit, but not discard the changes. Default rebasing doesn't show those merge commits.
我需要删除该合并提交,但不放弃更改。默认变基不显示这些合并提交。
回答by DIMMSum
If you want to apply the changes of the commits without including the merge commit (which as Roland Smith pointed out is generally considered bad practice) you can use git rebase
with the following workflow:
如果您想在不包括合并提交的情况下应用提交的更改(正如 Roland Smith 指出的那样,这通常被认为是不好的做法),您可以使用git rebase
以下工作流程:
- Checkout the branch you want the changes to be on (let's say master):
git checkout master
- Reset master to the commit before the merge you want to undo:
git reset --hard <hash of prior commit>
. Make sure there are no uncommitted changes on the branch you were working on or this command will wipe them out. - Checkout the branch containing the updates you want to "merge" to master without actually merging it (let's call it
dev
):git checkout dev
. - Rebase
dev
onto master. This will put theHEAD
commit of dev to be upstream to the most recent commit that master points to:git rebase master
(see the docsfor more information). - Fix any merge conflicts that result during the rebase (they would have happened in a normal merge too).
- The rebased branch should now have all of its commits following the latest commit on master. To port this over to master, check out master (
git checkout master
) and merge the new branchgit merge dev
. This will pull the changes onto master without adding a merge commit. - Once you're sure the updated
master
branch looks okay, you'll have to update the branch on any remote repositories too. Because you're undoing prior history, when you push the branch you'll have to use the--force
(or-f
) flag for the remote repo to accept the changes i.e.:git push origin master --force
.
- 签出您希望更改所在的分支(假设是 master):
git checkout master
- 在要撤消的合并之前将 master 重置为提交:
git reset --hard <hash of prior commit>
。确保您正在处理的分支上没有未提交的更改,否则此命令将清除它们。 - 检出含有“合并”掌握你想要更新的分支,而无需实际合并这(让我们叫它
dev
)git checkout dev
。 - 变基
dev
到主人。这会将HEAD
dev的提交置于master 指向的最新提交的上游:(有关更多信息,git rebase master
请参阅文档)。 - 修复在变基期间导致的任何合并冲突(它们也会在正常合并中发生)。
- 重新定位的分支现在应该在 master 上的最新提交之后进行所有提交。要将其移植到 master,请检查 master (
git checkout master
) 并合并新分支git merge dev
。这会将更改拉到 master 上,而无需添加合并提交。 - 一旦您确定更新的
master
分支看起来没问题,您还必须更新任何远程存储库上的分支。因为您正在撤消先前的历史记录,所以当您推送分支时,您必须使用--force
(or-f
) 标志让远程仓库接受更改,即:git push origin master --force
。
That's all there is to it. After you do this, the commits on dev
that were branched off of master should now be upstream to the pre-merge commit above.
这里的所有都是它的。执行此操作后,dev
从 master 分支的提交现在应该位于上面的预合并提交的上游。
NOTE: Running rebase will permanently update the commit history for the changed branches. For safety, I recommend taking 1 of 2 methods:
注意:运行 rebase 将永久更新已更改分支的提交历史记录。为了安全起见,我建议采用以下两种方法中的一种:
- Per @Dacav's suggestion, keep track of the commit hashes for each branch before you update anything. If things go pear shaped, then just run
git reset --hard <original_commit_hash>
to put the branch back on the original commit. Make backup copies of both branches before you rebase that are pointing at the original commit:
git checkout -b master_bk git checkout -b dev_bk
- 根据@Dacav 的建议,在更新任何内容之前,请跟踪每个分支的提交哈希。如果事情变成梨形,那么只需运行
git reset --hard <original_commit_hash>
将分支放回原始提交。 在指向原始提交的变基之前制作两个分支的备份副本:
git checkout -b master_bk git checkout -b dev_bk