0

I was in local repo. Stage + Commit, than Push to the server. Master Commit now is 4e4f27c554ee42e5fb7a0b62b6d921cbd05819c9.

I have made a mistake! Doh...

I'd like to return to the previous commit, returning to 5e814650f03d04e56fcb069000f77123c1b5273b, in both public e local repo.

How can I do it? Tried git push origin +5e814650f03d04e56fcb069000f77123c1b5273b:master, but on local repo I can still see the wrong Commit in Outgoing Commits window (Visual Studio Git Tool), and I don't know how to remove it.

What's the best way?

markzzz
  • 47,390
  • 120
  • 299
  • 507

1 Answers1

4

You can either use git revert or git reset as per your needs:-

  • Use git revert <commit_ID> - The reverted commit is not deleted. Rather, Git creates a new commit with the included files reverted to their previous state. So your version control history moves forward while the state of your files moves backwards.
  • Use git reset --hard <commit_ID> - Git discards any commits between the current state of the repo and the target commit. The branch will then appear to stop at the commit you reset the HEAD to. Although the commits no longer appear to be a part of your branch history, they are not deleted. They are still stored in Git.

After that, push your changes to remote (Assuming that your branch is called master both here and remotely, and that your remote is called origin):-

# note this will hard reset your commit
git reset --hard <commit-hash>
# pushing changes to remote
git push -f origin master
nandal
  • 2,544
  • 1
  • 18
  • 23
  • Uhm, what if I have edits (not staged yet)? That --hard will remove all of them also. Can I avoid it? – markzzz Jun 27 '18 at 11:50
  • 1
    @markzzz: staged or not, `git reset --hard` will destroy them, yes. The solution is to commit them. Of course this gets tricky because the commit goes on the current branch. The solution to *that* problem is sometimes `git stash`, which makes commits that are on *no* branch, and lets you recover the stashed changes later, after resetting the current branch or moving to a different one. (I'm not a big fan of `git stash` but this particular use case is a decent one.) – torek Jun 27 '18 at 18:00
  • I see, thanks! Also: "Although the commits no longer appear to be a part of your branch history, they are not deleted.". What if I'd like to delete them totally? Any other futher command? – markzzz Jun 28 '18 at 06:59