如何删除 git 中的中间合并?

发布于 2024-10-12 00:12:53 字数 1002 浏览 9 评论 0原文

我想删除中间合并(删除,而不是挤压),然后将最后 2 次提交移到新分支。

这是我当前的 git log --graph :

* 3a5c453 - (2 hours ago) last commit (HEAD, master)
*   b6c19f1 - (2 hours ago) Merge branch 'tade' into HEAD
|\  
* | be356d0 - (2 hours ago) previous commit
| * 65328dc - (3 hours ago) some other commit in branch tade

我想以这样的结果结束:

    * bbbbbbb - (some time in the future) a later commit on tade (tade)
*   | aaaaaaa - (some time in the future) a later commit on master (master)
| * | 3a5c453 - (2 hours ago) last commit (HEAD, newone)
| * | be356d0 - (2 hours ago) previous commit
|/  |
|   * 65328dc - (3 hours ago) some other commit in branch tade

我想到使用 git rebase -i 删除与分支tade的合并,然后执行gitbranchnewonegitreset --hard HEAD^2将最后2次提交移动到新分支。当我进行 rebase 时,它​​向我显示了来自 tade 分支的所有提交,这些提交已合并到 master 和 | 中。不愿意删除它们。

有更好的方法还是我应该继续?

编辑:我更新了预期的状态图以使其更加清晰。 2 个新提交(aaaaaaabbbbbbb)只是为了更好地说明状态(我希望)

I want to remove an in-between merge (remove, not squash) and then move the last 2 commits to a new branch.

This is my current git log --graph:

* 3a5c453 - (2 hours ago) last commit (HEAD, master)
*   b6c19f1 - (2 hours ago) Merge branch 'tade' into HEAD
|\  
* | be356d0 - (2 hours ago) previous commit
| * 65328dc - (3 hours ago) some other commit in branch tade

I want to end up with this:

    * bbbbbbb - (some time in the future) a later commit on tade (tade)
*   | aaaaaaa - (some time in the future) a later commit on master (master)
| * | 3a5c453 - (2 hours ago) last commit (HEAD, newone)
| * | be356d0 - (2 hours ago) previous commit
|/  |
|   * 65328dc - (3 hours ago) some other commit in branch tade

I thought of using git rebase -i to remove the merge with the branch tade and then do a git branch newone and git reset --hard HEAD^2 to move the last 2 commits to the new branch. When I did the rebase though, it showed me all the commits from the tade branch that got merged into master and | was reluctant in deleting them.

Is there a better way or should I go ahead with it?

EDIT: I updated the intended state graph to make it more clear. The 2 new commit (aaaaaaa and bbbbbbb) are only there to illustrate the state a little better (I hope)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掐死时间 2024-10-19 00:12:53

使用交互式变基在 be356d0 上变基 3a5c453,而不是合并提交 b6c19f1(即只需将 3a5c453 提交移至向下)。然后你应该有这样的东西:

*   xxxxxxx - (2 hours ago) Merge branch 'tade' into HEAD
|\  
* | yyyyyyy - (2 hours ago) last commit (HEAD)
* | be356d0 - (2 hours ago) previous commit
| * 65328dc - (3 hours ago) some other commit in branch tade

然后你可以创建新的分支:

git checkout -b newbranch yyyyyy

然后你可以删除 xxxxxx 并向 master 提交一些内容,最后得到这样的结果:

* zzzzzz - new commit on master
| * | yyyyyyy - (2 hours ago) last commit (HEAD)
| * | be356d0 - (2 hours ago) previous commit
| | * 65328dc - (3 hours ago) some other commit in branch tade

Use interactive rebasing to rebase 3a5c453 on be356d0 instead of the merge commit b6c19f1 (i.e. just move the 3a5c453 commit one down). Then you should have something like this:

*   xxxxxxx - (2 hours ago) Merge branch 'tade' into HEAD
|\  
* | yyyyyyy - (2 hours ago) last commit (HEAD)
* | be356d0 - (2 hours ago) previous commit
| * 65328dc - (3 hours ago) some other commit in branch tade

Then you can just create new branches:

git checkout -b newbranch yyyyyy

Then you can delete xxxxxx and commit something to the master and end up with this:

* zzzzzz - new commit on master
| * | yyyyyyy - (2 hours ago) last commit (HEAD)
| * | be356d0 - (2 hours ago) previous commit
| | * 65328dc - (3 hours ago) some other commit in branch tade
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文