310

how do i revert all my files on my local copy back to a certain commit?

commit 4a155e5b3b4548f5f8139b5210b9bb477fa549de
Author: John Doe <Doe.John.10@gmail.com>
Date:   Thu Jul 21 20:51:38 2011 -0500

This is the commit i'd like to revert back to. any help would be a lifesaver!

David
  • 10,418
  • 17
  • 72
  • 122
  • @WilliamPursell - Why did you delete your answer? Yours seems to be the one that is most sensible. After the reversion, the OP can commit and push (that is, he has a working repo). All the answers below put the repo in a state where nothing useful can be done with it. – jww Jul 06 '16 at 23:23

3 Answers3

474

git reset --hard 4a155e5 Will move the HEAD back to where you want to be. There may be other references ahead of that time that you would need to remove if you don't want anything to point to the history you just deleted.

Andy
  • 44,610
  • 13
  • 70
  • 69
  • 1
    I have been using this approach but what do you need to do in order to safely be able to commit on another machine? (instead `git pull -f origin master`) – Christophe De Troyer Jan 08 '16 at 15:30
  • @ChristopheDeTroyer I don't follow your question. – Andy Jan 08 '16 at 18:15
  • so this actually changes the history? if so i need to add it to my list of `git rebase` and `git commit --amend` – Randy L Apr 21 '16 at 16:31
  • @the0ther -- This doesn't change history, it just erases some of it. It's like going back in time. You don't change anything you just reset the point in time where you are at. – Andy Apr 21 '16 at 18:00
  • Thinking about it a little more and it would fall into the category of "changing history" especially if you follow it up with a forced push to the remote. – Randy L Apr 21 '16 at 18:44
  • 2
    @jww The question is "How do you revert back to a certain commit" How is this answer wrong? – Andy Jul 06 '16 at 21:46
  • 21
    You can't commit and push after you follow the advice. What good is a repo that you can't do anything with after the bad commits are backed out? – jww Jul 06 '16 at 22:53
  • 3
    You're making the assumption that the user has a remote repository that they are tracking and that they already pushed their bad commits to it. – Andy Jul 07 '16 at 21:13
  • 1
    Downvoting because OP specifically says "revert back to", and revert has a particular meaning in git; a reset can break history on a push, while a revert won't. – piersb Mar 19 '18 at 10:59
  • 13
    After 'git reset --hard ' on your local, if you try to commit your latest changes to remote branch, you will most likely get an error indicating that 'Your branch is behind origin'. When that happens, you need to force the push using an f tag 'git push -f' – JavaGeek Oct 24 '19 at 11:03
62

You can revert all your files under your working directory and index by typing following this command

git reset --hard <SHAsum of your commit>

You can also type

git reset --hard HEAD #your current head point

or

git reset --hard HEAD^ #your previous head point

Hope it helps

TheOneTeam
  • 25,806
  • 45
  • 116
  • 158
  • 2
    `revert` is not the correct command. `revert` applies a new commit that undoes a previous commit. It doesn't take a `--hard` option. – CB Bailey Jul 22 '11 at 18:42
  • @Charles: Why it is not correct? it does take the --hard option – TheOneTeam Jul 22 '11 at 23:17
  • 5
    Read the documentation, [`revert`](http://www.kernel.org/pub/software/scm/git/docs/git-revert.html) undoes the changes introduced by a single commit, it doesn't reset the index and working tree _to_ a particular commit which is what the asker is looking for. That is what [`reset`](http://www.kernel.org/pub/software/scm/git/docs/git-reset.html) does. `reset` does take a `--hard` option. – CB Bailey Jul 23 '11 at 06:41
  • Yet another wrong answer on Stack Overflow... You can't commit these changes. See [Commit and push changes after going back to a particular revision in the repository?](http://stackoverflow.com/q/38229852) – jww Jul 06 '16 at 17:29
  • 2
    This is a very bad approach. I just lost all my recent commits! – Mehdi Haghgoo Jan 03 '18 at 03:48
45

http://www.kernel.org/pub/software/scm/git/docs/git-revert.html

using git revert <commit> will create a new commit that reverts the one you dont want to have.

You can specify a list of commits to revert.

An alternative: http://git-scm.com/docs/git-reset

git reset will reset your copy to the commit you want.

TemporaryFix
  • 2,008
  • 3
  • 30
  • 54
marcelog
  • 7,062
  • 1
  • 33
  • 46