git ready

learn git one commit at a time
by Nick Quaranto

reverting files

committed 11 Jan 2009

This is a topic that is a constant source of confusion for many git users, basically because there’s more than one way to skin the proverbial cat. Let’s go over some of the basic commands that you’ll need to undo your work.

So, you just want to revert one file back to its original state:

git checkout <file>

One problem with this is that you may have is that a file and branch named the same. Since the checkout command is used for both reverting files and swapping out to a different branch, you’ll need to use this syntax (thanks, Norbauer)

git checkout -- <file>

If you want to throw out all of the changes you’ve been working on, there’s two ways to do that.

git checkout -f or git reset --HARD

Once these commands are run you’ll lose all of the work that isn’t committed in your directory, so make sure to take caution when using them.

Also, be aware that ‘git revert’ is not equivalent to ‘svn revert’! git-revert is used to reverse commits, something another tip will cover in the future.