kemenov
kemenov

Reputation: 407

Git - revert all commits of the merge after push

I have the following situation:

master A -  B -  E 
                / 
second   C -   D - F

What is the best solution to revert all commits of 'second branch' from master branch?

The 'second' branch has about 70 commits.

Upvotes: 3

Views: 6583

Answers (3)

Daniel Noguchi
Daniel Noguchi

Reputation: 613

I'm assuming 'E' is a merge commit. There are two simple ways to do this. One is to reset your last commit from branch master:

git checkout master
git reset --hard HEAD^

I don't recommend this, since, this will remove the last commit from your master branch.

Now the second option is to revert the commit:

git checkout master
git revert HEAD -m 1 M

You have to pass the -m option because 'E' is a merge commit. This will create a new commit reverting the changes made. I suggest you try this at a copy of the repository before anything else. Also, if you're working in collaboration with other developers, it is desirable not to rewrite history. The first method rewrites the history, so I suggest you use the second one.

Also, check these links:

how to revert a faulty merge

rewriting history

Hope it helps!

Upvotes: 6

eddiemoya
eddiemoya

Reputation: 7478

You should take a look at an answer I provided for a slightly different question, which involved reverting merge commits. "Reverting Merges vs. Resetting Merges". This will explain how revert works and how reset work with respect to merge commits. In that case someone had already reverted, but had some funky behavior after wanting to undo the revert - so take a look after reading this answer so you can get a full understanding of how it works.

Dont Reset!

Using git-reset after having pushed the commit means you've changed history that other people already have, This is bad. If you reset, you are effectively rewinding history. To push this up you will need to perform a forced push. Other developers will then need to know to use git-fetch followed by git reset --hard origin/master, and they may even need to perform a complicated git rebase --onto if they have new commits after the merge commit you removed. Other wise they will just end up pushing up the same commit you just reset.

It is generally a bad idea to modify history that has already been pushed.

Use Git Revert

Using git revert creates a new commit that negates the content of the merge commit instead of rewinding it. It moves you forward in history rather than modifying history.

git revert E

After reverting that merge commit, you will have the following history...

master A -  B -  E  - G 
                / 
               /
second   C -  D - F

TheG commit represents the revert. That commit "undoes" the changes that the merge introduced. There are issues to be aware of when reverting merge commits. Please read my answer on "Reverting Merges vs. Resetting Merges" to become aware of how it works, and how to undo the revert later.

Upvotes: 11

Kit Ho
Kit Ho

Reputation: 26968

To revert it, it is very easy, commit E is the merge commit from all commit in second branch, what to do is to delete the E (merge commit), then in master branch, there will be no more things from second branch.

  1. git checkout master
  2. git reset --hard HEAD^

Then it is ok.

Everything in second branch will still retain. For master branch, no more things from second branch.

Upvotes: 0

Related Questions