112

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?

2 Answers 2

195

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
8
  • 5
    This way all the branches will be pushed with force. You may want to try git push --force origin master
    – danza
    Commented Apr 11, 2013 at 7:42
  • 1
    For reference, to revert to the previous commit, you can do a git reset --hard HEAD^
    – Geoff
    Commented Sep 6, 2013 at 2: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
    Commented Jul 27, 2015 at 8: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
    Commented Jan 26, 2016 at 5:29
  • A little advice after this revert, could be tell to the team : pull from master branch!! Thanks!!
    – JRichardsz
    Commented Jul 27, 2016 at 15:36
136

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
8
  • 30
    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. Commented Oct 13, 2016 at 20:46
  • 2
    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 <stdin>:55: trailing whitespace. <stdin>:336: trailing whitespace. <stdin>:12692: trailing whitespace. <stdin>:12695: trailing whitespace. <li>{{ $tag['rank']+1 }}: <stdin>:12706: trailing whitespace. error: patch failed: .env.wholo:1
    – rickatech
    Commented Dec 19, 2016 at 20:42
  • I've tried several solutions, but none of the worked. This one worked like a charm. Thanks a lot @John
    – Strabek
    Commented Jun 19, 2018 at 15:23
  • 1
    This is the correct way to do. It's non-destructive and preserves full history Commented Apr 15, 2020 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? Commented Jun 18, 2020 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.