Rolling back a remote Git repository

Git

Git Problem Overview


I have a remote Git repository, and I need to roll back the last n commits into cold oblivion.

Git Solutions


Solution 1 - Git

You can use git revert <commit>… for all the n commits, and then push as usual, keeping history unchanged.

Or you can "roll back" with git reset --hard HEAD~n. If you are pushing in a public or shared repository, you may diverge and break others work based on your original branch. Git will prevent you doing so, but you can use git push -f to force the update.

Solution 2 - Git

elmarco is correct... his suggestion is the best for shared/public repositories (or, at least public branches). If it wasn't shared (or you're willing to disrupt others) you can also push a particular ref:

git push origin old_master:master

Or, if there's a particular commit SHA1 (say 1e4f99e in abbreviated form) you'd like to move back to:

git push origin 1e4f99e:master

Solution 3 - Git

Fortunately I was in a position to use Pat Notz's solution which completely removed the unwanted commit. However, initially I got the error

error: failed to push some refs to 'ssh://[email protected]'
To prevent you from losing history, non-fast-forward updates were rejected*

But adding the force (-f) option overwrite this error

git push -f origin 52e36b294e:master

Solution 4 - Git

If you have direct access to the remote repo, you could always use:

git reset --soft <sha1>

This works since there is no attempt to modify the non-existent working directory. For more details please see the original answer:

https://stackoverflow.com/questions/4624881/how-can-i-uncommit-the-last-commit-in-a-git-bare-repository/5627799#5627799

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJakeView Question on Stackoverflow
Solution 1 - GitelmarcoView Answer on Stackoverflow
Solution 2 - GitPat NotzView Answer on Stackoverflow
Solution 3 - GitgeedoubleyaView Answer on Stackoverflow
Solution 4 - GitHazokView Answer on Stackoverflow