Reverting changes in Git

As I wrote earlier, I started using Git pretty intensively lately and I had to find out the hard way that some commands behave quite differently from what I was used to with Subversion.

One such command, which I use pretty often, is revert. In Subversion, this command will undo all your local edits, essentially reverting the state of your working directory to match the repository. Not in Git.

With Git, issuing the revert command, means reverting a previous commit instead. This can actually be pretty useful when you want to roll back changes you have already committed (there is no easy way to do this in Subversion), but sometimes you just want to trash all uncommitted changes with no questions asked (and no trace in the commit log).

The command that does this is called reset *. The reset command, unsurprisingly, resets the state of your working directory to match the repository (sounds familiar?). It takes a commit, such as HEAD for the current revision, as a parameter and an optional tag to specify how the reset should take place. If you want to really replace any locally modified files by the originals from the repository, use the –hard flag. This makes the command look like this:

git reset --hard HEAD

which behaves pretty similar to svn revert.

What this does not do, is clean up any files in your working directory that were never added to the repository in the first place. This sometimes happens to me when I mistype a Ruby on Rails generate command, which creates a lot of files containing the same mistake. Luckily, git also has a command to fix this: clean. The command

git clean -df

will remove all files and directories unknown to git. Watch out (using git status) that this does not accidentally delete too much, such as sqlite3 databases that you forgot to add to .gitignore.

Git’s revert and reset behaviour may not always be the same on a per command basis, the functionality is there. You just have to use the right commands :)

*: Actually, the reset command is a lot more powerful, but you should check out the documentation for that.

9 Responses to “Reverting changes in Git”

  1. Steffen says:

    Bravo. I had the EXACT same problems, all of them: came from svn, started using git for rails, had to roll back generator output and changes. Thanks!

  2. Joao says:

    Thanks, this was useful

  3. Sanjeev says:

    Does it really reset the working directory alone or the whole repository!? I mean, I have literally screwed up one of the working directory in the git tree and I just want to revert that directory alone. Could there be any command for this? Thank you very much for the article. The new thing I came to know is “clean -df”. Thank you.

  4. Sanjeev says:

    Thank you, I’ve found it out:
    “git ls-files -d | xargs git checkout –“

  5. Hey there this is kinda of off topic but I was wondering if blogs
    use WYSIWYG editors or if you have to manually code with HTML.
    I’m starting a blog soon but have no coding knowledge so I wanted to
    get advice from someone with experience. Any help would be enormously appreciated!

  6. Hiya very nice blog!! Man .. Excellent .. Wonderful ..
    I’ll bookmark your web site and take the feeds additionally?
    I am happy to search out so many helpful info here within the submit,
    we need work out extra strategies on this regard,
    thank you for sharing. . . . . .

  7. Teresa says:

    Hey there great website! Does running a blog like this
    require a great deal of work? I’ve absolutely no knowledge
    of coding however I was hoping to start my own blog in the near future.
    Anyways, if you have any recommendations or tips for new blog owners please share.
    I understand this is off topic but I just wanted to ask. Cheers!

  8. What’s up i am kavin, its my first occasion to commenting anyplace, when i read this post
    i thought i could also create comment due to this good paragraph.

  9. Greetings from Florida! I’m bored to death at work so I decided to browse your
    website on my iphone during lunch break. I love the knowledge you provide here
    and can’t wait to take a look when I get home.
    I’m amazed at how fast your blog loaded on my cell phone ..
    I’m not even using WIFI, just 3G .. Anyways, good site!