Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I revert master branch to a tag in git?

Tags:

git

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?

like image 670
Manjunath Manoharan Avatar asked Jul 29 '11 11:07

Manjunath Manoharan


People also ask

How do I revert from master branch?

When you want to revert to a past commit using git reset – – hard, add <SOME-COMMIT>. Then Git will: Make your present branch (typically master) back to point at <SOME-COMMIT>. Then it will make the files in the working tree and the index (“staging area”) the same as the versions committed in <SOME-COMMIT>.


2 Answers

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 
like image 27
John Avatar answered Sep 20 '22 13:09

John


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 
like image 133
Paweł Obrok Avatar answered Sep 20 '22 13:09

Paweł Obrok