334

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!

1
  • @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. Commented Jul 6, 2016 at 23:23

3 Answers 3

507

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.

Sign up to request clarification or add additional context in comments.

11 Comments

@jww The question is "How do you revert back to a certain commit" How is this answer wrong?
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?
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.
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.
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'
|
67

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

5 Comments

revert is not the correct command. revert applies a new commit that undoes a previous commit. It doesn't take a --hard option.
@Charles: Why it is not correct? it does take the --hard option
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.
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?
This is a very bad approach. I just lost all my recent commits!
49

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.

2 Comments

Safer than git reset --hard, and the latter is cleaner.
git revert would not work for merges

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.