111

We have branches origin and develop. The initial state of master was tagged at tag_ABC.

We have few changes made to the develop branch and pushed to origin. Then we have accidentally merged develop into master and pushed to origin.

Now we would like to revert master to the checkpoint tag_ABC. How can we do that?

Dom
  • 1,687
  • 6
  • 27
  • 37
Manjunath Manoharan
  • 4,567
  • 6
  • 28
  • 43

2 Answers2

187

You can do

git checkout master
git reset --hard tag_ABC
git push --force origin master

Please note that this will overwrite existing history in the upstream repo and may cause problems for other developers who have this repo checked out.

As per Luke Wenke's comment, other developers who have got master checked out will have to do the following:

git pull
git reset --hard origin/master
icc97
  • 11,395
  • 8
  • 76
  • 90
Paweł Obrok
  • 22,568
  • 8
  • 74
  • 70
  • 5
    This way all the branches will be pushed with force. You may want to try `git push --force origin master` – danza Apr 11 '13 at 07:42
  • 1
    For reference, to revert to the previous commit, you can do a `git reset --hard HEAD^` – Geoff Sep 06 '13 at 02:57
  • 1
    BTW `git reset --hard HEAD^` can be used multiple times to step back one commit at a time then if it is on remote, `git push --force origin master` can be used. – Luke Wenke Jul 27 '15 at 08:19
  • 1
    Also to reset the master branch of other people's local copies to the earlier version use `git pull` and `git reset --hard origin/master` – Luke Wenke Jan 26 '16 at 05:29
  • A little advice after this revert, could be tell to the team : pull from master branch!! Thanks!! – JRichardsz Jul 27 '16 at 15:36
  • does this actually "revert", as in leave the commit history? I have a feeling you're confusing revert and reset – Emobe Jun 02 '20 at 12:03
  • @Emobe while "revert" is indeed a git term for "unapplying" a commit, the question seemed more interested in restoring the previous state of the branches, hence this answer. – Paweł Obrok Jun 03 '20 at 23:19
  • A note about this method: When you are using a protected master branch, you can't reset that branch. You have to unprotect the branch first, reset and protect it again. – Andrés Morales Jun 29 '22 at 12:55
132

This isn't a direct answer to the question but this page comes back when searching for ways to revert a branch's code to a tag release.

Another way is to create a diff between the current state of the branch and the tag you want to revert to and then apply that to the branch. This keeps the version history correct and shows the changes going in then coming back out again.

Assuming your branch is called master and the tag you want to go back to is called 1.1.1

git checkout 1.1.1
git diff master > ~/diff.patch
git checkout master
cat ~/diff.patch | git apply
git commit -am 'Rolled back to version 1.1.1'
git push origin master
John
  • 1,688
  • 1
  • 12
  • 9
  • 28
    This should be the accepted answer since it keeps the history intact and doesn't cause problems for others who have the repo checked out. – OpenUserX03 Oct 13 '16 at 20:46
  • 1
    This seems elegant. But I tried to revert qa branch to a previous tag and just got patch fail messages. $ cat ../diff_qa.patch | git apply :55: trailing whitespace. :336: trailing whitespace. :12692: trailing whitespace. :12695: trailing whitespace.
  • {{ $tag['rank']+1 }}: :12706: trailing whitespace. error: patch failed: .env.wholo:1
  • – rickatech Dec 19 '16 at 20:42
  • I've tried several solutions, but none of the worked. This one worked like a charm. Thanks a lot @John – Strabek Jun 19 '18 at 15:23
  • 1
    This is the correct way to do. It's non-destructive and preserves full history – Nitin Bansal Apr 15 '20 at 14:31
  • This does not work for me. I can run the first 3 commands without any issue, but when I do ``cat ~/diff.patch | git apply``, it tells me ``error: unrecognized input``. Any idea? @NitinBansal perhaps since you commented recently? – Experience111 Jun 18 '20 at 10:16
  • Just a guess but sounds like a problem with your git config and how it produces patch files. Try https://stackoverflow.com/questions/37347350/all-git-patches-i-create-throw-fatal-unrecognized-input – John Jul 13 '20 at 08:28
  • I just wonder why this isn't a built-in git command. This is probably the most basic feature you would expect by any VCS – Christian Vincenzo Traina Mar 03 '23 at 17:17