maan81
maan81

Reputation: 3609

Removing branch merged in history?

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

Upvotes: 2

Views: 153

Answers (2)

kfb
kfb

Reputation: 6532

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

Upvotes: 1

VonC
VonC

Reputation: 1325357

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)

Upvotes: 1

Related Questions