Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undoing the merge in Git so that no commits from the other branch exist

I've two branches: development, and bug_fixes, and I accidentally merged development branch in bug_fixes two to three weeks ago, and have been working on bug_fixes since then, and I've pushed the changes in bug_fixes.

Now, I need to un-merge the development branch. I want a pure bug_fixes branch with no commits form development branch. By googling, I've come up with the following scenarios:

1) Revert the commit that merged development branch in bug_fixes branch, but it partially did the job. The commits from development branch can still be seen in bug_fixes branch, something that isn't required.

2) Rebase, and delete the commit that did the merging, but it still has the same issue described in point 1. bug_fixes branch still contains the commits from development branch.

Is there a way I can delete the merging commit, and all those commits that came in bug_fixes branch form development branch, would be deleted as well? Or, it isn't possible?

like image 427
Arslan Ali Avatar asked Apr 10 '16 16:04

Arslan Ali


People also ask

Can a merge be undone in git?

You can use the Git reset command to undo a merge. Firstly, you need to check for the commit hash (or id) so you can use it to go back to the previous commit. To check for the hash, run git log or git reflog . git reflog is a better option because things are more readable with it.

What happens when you revert a merge commit?

REVERTING THE MERGE COMMIT To revert the changes brought in by the feature branch, revert the commit with respect to the second parent (1484b1a). This will revert all the changes made by the second branch (feature) on master. The resulting tree will behave as the branch feature was never merged to master.

How do you terminate a merge?

How do I cancel a git merge? Use git-reset or git merge --abort to cancel a merge that had conflicts. Please note that all the changes will be reset, and this operation cannot be reverted, so make sure to commit or git-stash all your changes before you start a merge.

How do you revert a merge commit that's already pushed to remote branch?

Now, if you have already pushed the merged changes you want to undo to your remote repository, you can right-click on the merge commit and select Revert commit from the context menu. You will then be asked if you want to immediately create a commit.


3 Answers

IF you know which commits are coming from the development branch (in addition of the merge commit), you can do a git rebase -i (interactive rebase) from the last good commit of bug fixes:

 b--b--(B)--M--d--b--d--b--b (bugfix)
           /
    d--d--d  (devel)

git checkout bugfix
git rebase -i (B)
# drop M, d, and d

 b--b--(B)--b'--b'--b' (bugfix)

    d--d--d  (devel)

The more complex alternative would be a git filter-branch, but hopefully, the interactive rebase is enough.

In the OP's case, the full command was:

git rebase -p -i (b)

-p allows to preserve the merge commit.

like image 158
VonC Avatar answered Oct 17 '22 08:10

VonC


I would try the following approach:

First of all, make a list of the commits in both branches:

git checkout development
git log --oneline > log-devel.txt

git checkout bug_fixing
git log --oneline > log-bugfixing.txt

Second, identify in each branch the id of the last commit before executing the merge. Let's name these commits devel-0 and bugfixing-0 Then, take your branches to the status they were before the accidental merge:

git checkout development
git reset --hard <devel-0-hash>

git checkout bug_fixing
git reset --hard <bugfixin-0-hash>

At this point you have two clean but outdated git branches. Now, you should take from log-devel.txt and log-bugfixing.txt all the commits that are not included in the current branches, and add them to the corresponding branch. In order to add a commit with id abc0123 to your branch development you can use cherry pick:

git checkout development
git cherry-pick `abc0123`

Take into account that the commits should better be added to the clean branches in the same order they were added to the bug fixing branch, in order to minimize the number of conflicts appearing.

like image 21
Bustikiller Avatar answered Oct 17 '22 08:10

Bustikiller


you need to know your latest commit message for bug_fixes before merge, if you know the sha hash then evne better.

now first you need to find the sha hash using the command

$ git reflog

the output is something like

1fb5738 HEAD@{10}: commit...

4d47df6 HEAD@{11}: commit...

5f32c4b HEAD@{12}: merge...

428f91d HEAD@{13}: checkout...

5f32c4b HEAD@{14}: checkout...

the first part is the sha hash. In this case the state before merge has hash value 428f91d

so now, in bug_fixes branch we reset to this hash value

git reset --hard 428f91d

now it is undone

like image 31
krazedkrish Avatar answered Oct 17 '22 09:10

krazedkrish