2

How could I remove a branch I had merged into master in the past ?

From something like :

master ... a---> b ---> c ---------> d -----> e ---> f ---> g ---> h \ / x ---> y ------> z branch1 

to :

... a---> b ---> c ---------> d -----> f ---> g ---> h \ x ---> y ---> z branch1 

I want to undo/remove merging of a branch into master I had done sometime earlier.


I have tried out something like but am getting conflicts. Is it possible ?

# currently we are in <h> commit in master branch git checkout master # new temporary branch git branch tmp_master git checkout tmp_master # reseting temp. branch to reqd. commit git reset <d> --hard # cherry-picking subsequent commits git cherry-pick <f>..<h> 

After it was done --as I had expected to do :

# change temp. branch to master branch git branch -D master git branch -m master 
7
  • 3
    Look into rebasing Commented Sep 15, 2016 at 11:50
  • 1
    Seconded. Do an interactive rebase of master and delete commit e. Ditto with branch1. Commented Sep 15, 2016 at 11:51
  • Check out this question for a similar situation: stackoverflow.com/questions/17577409/… Commented Sep 15, 2016 at 11:52
  • Although if branch 1 hasn't moved after the commit, you can just reset it back one. Commented Sep 15, 2016 at 11:52
  • 1
    You're getting errors or conflicts? If actual errors, then please share those, otherwise your approach is sound and likely what I would do. Commented Sep 15, 2016 at 11:53

2 Answers 2

1

git rebase --onto d e will remove e from the branch history of h. See here for more information.

Sign up to request clarification or add additional context in comments.

2 Comments

As there number of commits, as I go resolving a conflict & rebase --continue, conflict for the next commit appears. Do I need to handle the same conflict in each step ?
If subsequent commits depend upon something that was removed in e, then you will have to fix them up individually as far as I'm aware. However, someone else may know of a faster way (that I would definitely be interested in myself!).
1

You either need to:

  • rebase --interactive (which would allow you to drop "e", the merge commit),
  • or git revert -m 1 e (see "Undoing Merges "), which creates a new commit cancelling the changes introduced by the merge.

The second solution allows for a simple push (you are pushing new commits), while the first solution would require a push --force (problematic in case of multiple user collaborating on the same remote repo)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.