Focus on IT Recommend

Home > github - Undoing the merge in Git so that no commits from the other branch exist

github - 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?

git github version-control branch branching-and-merging
  this question
edited Apr 13 '16 at 11:28 asked Apr 10 '16 at 16:40 Arslan Ali 10.2k 6 24 45 1   why not you create an another third branch future and use cherrypick to do your work. –  VKatz Apr 18 '16 at 16:14      Read here how to "cancel" the commits you want.… –  CodeWizard Apr 19 '16 at 5:36


4 Answers

---Accepted---Accepted---Accepted--- +100

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.

  this answer
edited Apr 20 '16 at 6:26 answered Apr 12 '16 at 19:18 VonC 669k 216 1971 2099      Here is the thing: I can see commits from development branch into bug_fixes branch, way behind the merging commit. It's not like that I'd have to remove only commits after the merging commit; there are commits from development branch that exist in bug_fixes way before it hits merging commit. It's like whole history in bug_fixes has been re-written. –  Arslan Ali Apr 13 '16 at 11:25      @ArslanAli then check your git reflog ( to see if you can detect why bug_fixes would have been rewritten. –  VonC Apr 13 '16 at 11:33      I'd definitely have a look at that branch through git reflog. One more thing: I can't see my merging commit in git rebase. –  Arslan Ali Apr 13 '16 at 12:45      @ArslanAli did you do a git rebase -i with the right commit (the one before the merge commit)? –  VonC Apr 13 '16 at 13:42      Yes, I exactly did it the way you are saying in comment. –  Arslan Ali Apr 13 '16 at 14:35  |  show more comments

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.

  this answer
answered Apr 16 '16 at 10:43 Bustikiller 1,297 1 6 19      Here's the issue: I can't just cherry-pick the commits, as bug_fixes branch includes commits from development branch way behind the merging commit. So where should I start? –  Arslan Ali Apr 20 '16 at 1:35


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...

Recommend:github - How do I merge a specific commit from one branch into another in Git

commits from BranchA merged into BranchB. Is there a way to do this git github
  this question edited Jun 16 '15 at 16:11 BuZZ-dEE 889 1 21 37 asked Jun 16 '11 at 12:46 NullVoxPopuli 13.9k 53 143 272 3 possible duplicate o

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

  this answer
answered Apr 18 '16 at 6:05 krazedkrish 359 1 7


Let me describe the scenario with an example below

Bug Fix branch: Revision 10

Merged Dev Branch: Revision 11

Multiple changes after merge: Revision 12 - Revision 15


Now there are 2 steps that needs to be done(Easy method Without git native commands)

  1. Go back to Revision 10

    Clone Latest code to a folder called "latest"

    Clone Revision 10 to a folder called "rev10"

    Remove all the content(except .git folder) from latest folder

    Copy all the content(except .git folder) from rev10 folder to latest folder

    Checking the latest folder content

Now the Bug fix branch will be having the content equal to Revision 10 without any developer branch changes

  1. Optional - Merging Revision 12 - Revision 15 to bug_fix branch

    As we have revert back to the old revision the latest commits to bugfix branch is also gone This revisions needs to be manually merged one by one back to bug fix branch

  this answer
answered Apr 19 '16 at 7:18 Ganesan Srinivasan 214 1 5


Recommend:version control - Show all commits in a git branch since original branching point from master

luding it) and hopefully since branching from master. For example situation like: A-B-C-D (master) \ E-F (branch A) I want to get commits F, E and B while F is the HEAD. And for A-B-C-D (master) \ E-F (branch B) \

------splitte line----------------------------