开发者

How to remove an in-between merge in git?

开发者 https://www.devze.com 2023-02-05 11:36 出处:网络
I want to remove an in-between merge (remove, not squash) and then move the last 2 commits to a new branch.

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
| * 65328d开发者_开发问答c - (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)


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号