如何在保留合并更改的同时删除合并提交? [英] How to remove merge commits while keeping merged changes?

查看:55
本文介绍了如何在保留合并更改的同时删除合并提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从回购中获得了提交日志.您可以看到将分支合并到...中".提交.这些提交之后,分支被合并.

I have our commit log from my repo. You can see the "Merge branch...into..." commits. After these commits, the branch was merged.

我需要删除合并提交,但不放弃更改.默认的重新设定基准不会显示合并提交.

I need to delete the merge commits, but not discard the changes. Default rebasing doesn't show merge commits.

推荐答案

如果您要应用提交的更改而不包含合并提交(如Roland Smith指出的那样,通常被认为是不好的做法),则可以使用 git rebase :

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:

  1. 签出您要启用更改的分支(比如说master): git checkout master
  2. 将主服务器重置为要撤消的合并之前的提交: git reset --hard<先前提交的哈希值.确保您正在处理的分支上没有未提交的更改,否则此命令将清除它们.
  3. 签出包含要合并"到母版的更新而不实际合并的分支(我们称其为 dev ): git checkout dev .
  4. dev 重置为master.这会将dev的 HEAD 提交置于master指向的最新提交的上游: git rebase master (请参阅
  1. Checkout the branch you want the changes to be on (let's say master): git checkout master
  2. 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.
  3. Checkout the branch containing the updates you want to "merge" to master without actually merging it (let's call it dev): git checkout dev.
  4. Rebase dev onto master. This will put the HEAD commit of dev to be upstream to the most recent commit that master points to: git rebase master (see the docs for more information).
  5. Fix any merge conflicts that result during the rebase (they would have happened in a normal merge too).
  6. 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 branch git merge dev. This will pull the changes onto master without adding a merge commit.
  7. 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.

仅此而已.完成此操作后,从主分支分支出来的 dev 上的提交现在应该在上面的合并前提交的上游.

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.

注意:运行rebase将永久更新已更改分支的提交历史记录.为了安全起见,我建议您采用2种方法之一:

NOTE: Running rebase will permanently update the commit history for the changed branches. For safety, I recommend taking 1 of 2 methods:

  1. 根据@Dacav的建议,在更新任何内容之前,请跟踪每个分支的提交哈希值.如果事情变糟了,那么只需运行 git reset --hard< original_commit_hash> 将分支放回原始提交即可.
  2. 在重新定位指向原始提交的两个分支之前,请制作两个分支的备份副本:

  1. 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.
  2. Make backup copies of both branches before you rebase that are pointing at the original commit:

git checkout -b master_bkgit checkout -b dev_bk

git checkout -b master_bk git checkout -b dev_bk

这篇关于如何在保留合并更改的同时删除合并提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆