I have a remote Git repository, and I need to roll back the last n
commits into cold oblivion.
4 Answers
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.

- 31,633
- 21
- 64
- 68
-
5You can rollback to a specific commit by using: `git reset --hard [sha1]` where sha1 is the commit hash identifier. – Pikachu Jul 17 '12 at 22:42
-
7You cannot use get reset --hard in a remote repository since there is no working directory. The original question only states there is a remote repo, there is no mention of a local repo. – Hazok Jun 13 '13 at 07:58
-
3just a note, git push -f will force push all local branches to their remotes – cowlinator Jan 09 '15 at 23:26
-
Please keep in mind, that using `revert` will cause your feature branches to be treated as "already" merged. That's because these branches *are* actually being merged. But the changes were reverted. Solution: _Cherry-pick_ or _revert the revert commit_. – Benedikt Feb 05 '16 at 11:45
-
@Benedikt does the `reset --hard` also cause the feature branches to be treated as already merged? My assumption is it wouldn't (where `revert` would). – Marcus Leon Apr 09 '16 at 12:11
-
@MarcusLeon `reset --hard` is totally fine. But applying this on a remote branch may cause troubles on team members' local branches. Make sure you align with your team and notify everyone so that they can update they local branch. – Benedikt Apr 10 '16 at 19:18
-
If you instead of using `git push -f` use `get push ---force-with-lease` you will avoid accidentally overwriting commits that have been pushed after your last `pull`. – Niemi Aug 24 '17 at 13:40
-
@pisaruk where to find the [sha1] to rollback a commit, is it something like "17f30ee4e8d35b7a54d12615e508f78532611097" – Eswar Sep 21 '18 at 05:24
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

- 22,495
- 29
- 154
- 227

- 208,672
- 30
- 90
- 92
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://git@gitrepo.git'
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

- 1
- 1

- 488
- 6
- 12
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:
How can I uncommit the last commit in a git bare repository?
-
2Why would `--soft` even be necessary? You could probably do the same thing with just plain `git reset`, without the mode flag. – Jun 24 '14 at 18:37