Rolling back in Git

后端 未结 3 1211
忘了有多久
忘了有多久 2021-01-31 03:59

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\" t

3条回答
  •  失恋的感觉
    2021-01-31 04:59

    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.

提交回复
热议问题