git revert back to certain commit

410,316

Solution 1

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.

Solution 2

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

Solution 3

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.

Share:
410,316

Related videos on Youtube

David
Author by

David

Updated on July 08, 2022

Comments

  • David
    David almost 2 years

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

    commit 4a155e5b3b4548f5f8139b5210b9bb477fa549de
    Author: John Doe <[email protected]>
    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!

    • jww
      jww almost 8 years
      @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.
  • CB Bailey
    CB Bailey almost 13 years
    revert is not the correct command. revert applies a new commit that undoes a previous commit. It doesn't take a --hard option.
  • TheOneTeam
    TheOneTeam almost 13 years
    @Charles: Why it is not correct? it does take the --hard option
  • CB Bailey
    CB Bailey almost 13 years
    Read the documentation, revert 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 does. reset does take a --hard option.
  • Christophe De Troyer
    Christophe De Troyer over 8 years
    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)
  • Andy
    Andy over 8 years
    @ChristopheDeTroyer I don't follow your question.
  • Randy L
    Randy L about 8 years
    so this actually changes the history? if so i need to add it to my list of git rebase and git commit --amend
  • Andy
    Andy about 8 years
    @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.
  • Randy L
    Randy L about 8 years
    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.
  • jww
    jww almost 8 years
    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?
  • Andy
    Andy almost 8 years
    @jww The question is "How do you revert back to a certain commit" How is this answer wrong?
  • jww
    jww almost 8 years
    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?
  • Andy
    Andy almost 8 years
    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.
  • Mehdi Haghgoo
    Mehdi Haghgoo over 6 years
    This is a very bad approach. I just lost all my recent commits!
  • piersb
    piersb over 6 years
    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.
  • JavaGeek
    JavaGeek over 4 years
    After 'git reset --hard <hash>' 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'
  • DawnSong
    DawnSong almost 4 years
    Safer than git reset --hard, and the latter is cleaner.
  • Stepan Yakovenko
    Stepan Yakovenko about 2 years
    git revert would not work for merges