Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rolling back in Git

Tags:

I have committed, and pushed, several patches: A1-->A2-->A3-->A4 (HEAD)

Everyone's pulled these changesets into their local copy.

Now we want to "roll back" to A2, and continue developing from there - essentially throwing away A3 and A4. What's the best way to do this?

like image 423
Gwyn Morfey Avatar asked Oct 20 '08 10:10

Gwyn Morfey


People also ask

How do I roll back git pull?

There is no command to explicitly undo the git pull command. The alternative is to use git reset, which reverts a repository back to a previous commit. We're working on a project called ck-git. A collaborator has just pushed a commit to the remote version of the project that is stored on GitHub.


2 Answers

From the root directory of your working copy just do

git checkout A2 -- .   git commit -m 'going back to A2' 

Using git revert for this purpose would be cumbersome, since you want to get rid of a whole series of commits and revert undoes them one at a time.

You do not want git reset either. That will merely change your master branch pointer: you are left with no record of the mistaken direction. It is also a pain to coordinate: since the commit you changed master to is not a child of the remote repository’s master branch pointer, pushing will fail – unless you add -f (force) or delete the master branch in the remote repository first and recreate it by pushing. But then everyone who tries to pull will still have the old history in their local master branch, so once origin/master diverges, git pull will try to perform a merge. This is not the end of the world: they can get out of this situation by doing git rebase --onto origin/master $old_origin_master_commit master (ie. rebase their local commits made on top of the old origin/master onto the top of the new origin/master). But Git will not know to do this automatically so you have to coordinate with every collaborator. In short, don’t do that.

like image 178
Aristotle Pagaltzis Avatar answered Oct 04 '22 02:10

Aristotle Pagaltzis


Throwing away those commits will likely have some negative effects on anyone who is pulling from your repository. As another option, you may want to consider creating an alternate development branch starting at A2:

 A1-->A2-->A3-->A4 (master/HEAD)       \        -->B1-->B2 (new-master/HEAD) 

Doing this is as simple as

 git branch new-master master~2 
like image 30
Pat Notz Avatar answered Oct 04 '22 03:10

Pat Notz