Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverting bad branch merge

Tags:

git

I merged my branch into master, and pushed, thinking all was well. Turns out the code has issues, and I would like a way to revert either the merge commit, or the entire branch, whichever is more appropriate, so that others can easily continue working as if my merge never happened.

Then I intend to fix my branch, and merge it in again. Reading man git-revert suggests that I will be able to re-merge if I use -m to revert the entire branch.

like image 526
Letharion Avatar asked Mar 13 '12 11:03

Letharion


1 Answers

Just reset HEAD to previous commit:

git reset --hard HEAD^

You don't state in your question if you pushed your faulty merge or not, so must push with --force if this is the case (since you have rewritten history on the master branch).

Note that rewriting history might cause problems for the other devs if they have based their work on top of the faulty commit. In this case you might want to consider:

git revert HEAD

However, this will create a new commit that does the opposite of the commit that HEAD points to right now and will taint the repository. Resetting is best in this sense since you get rid of two commits, one faulty and one to compensate for the faulty.

like image 94
ralphtheninja Avatar answered Sep 16 '22 18:09

ralphtheninja