Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Undoing Things in Git

DZone 's Guide to

Undoing Things in Git

Don't cry over spilled milk. Go grab a command line and get to cleaning up.

· Open Source Zone ·
Free Resource

Most people have no idea how dangerous life as a software developer can be: You can delete the wrong files, code into a completely wrong direction, or mess up the whole project with a single commit. Therefore, I find it invaluable to have a strong safety net for those many times when things go wrong. Using Git as your version control system, you have such a safety net – and a very powerful one indeed.

In this article, I’ll give you a short introduction of how Git can help you undo mistakes.

Fix Your Last Commit

It’s really easy to mess up a commit: making typos in your commit message or forgetting to add a change to the staging area are just two examples. Often, we notice our mistake right after we hit the enter key.

Thankfully, Git makes it very easy to fix the very last commit. Let’s pretend we had just hit enter after typing the following command:

$ git commit -m "Massage full of typohs"


As if the typos in this message weren’t already bad enough, let’s say we also forgot to add another changed file to the staging area. We can fix both mistakes by using the following two commands:

$ git add forgotten-changes.js
$ git commit --amend -m "A useful message"


The --amend flag is the important bit in this example: Git will fix the very last commit with your new message and any changes you might have staged.

There’s only one thing to keep in mind: you should only use --amend on local commits that you haven’t yet pushed to a remote repository. The reason for this is that --amend rewrites the given commit, replacing it entirely with the fixed version. After that, it seems as if the original commit had never happened — which is exactly what we intend. But only if we haven’t published this commit, yet!

Undo Local Changes

Let’s say you have a more drastic case to take care of: your latest code changes have proven to be… how do I say it… not optimal. In short: you want to undo your work and start over at the last committed revision.

An example situation could look like this:

$ git status
modified:   about.html
deleted:        imprint.html
modified:   index.html


Let’s say that you had better kept your hands off of about.html and had better not deleted imprint.html. To undo your mistakes, we’d like to discard the changes in these two files - but keep the (awesome!) changes in index.html.

The “git checkout” command lets us do just this:

$ git checkout HEAD about.html imprint.html


With this command, we can restore about.html and imprint.html to their last committed states.

In cases where not all of your changes in a certain file are bad, you might consider discarding just individual lines in a changed file. Yep, that’s possible — but rather complicated to do on the command line. Have a look at how a desktop Git client like “Tower” can do this:

Discard Individual Lines in Git Tower

When the situation is really bad — like “shouldn’t have started coding in the first place” bad — then you can also take out the big gun:

$ git reset --hard HEAD


With the git checkout command, we restored specific files. With the git reset command, on the other hand, we can restore our whole working copy! Or, to put it another way: this will reset your complete project to the state that was last committed.

A short word of warning for this tool, too: when you discard local changes with these commands, you cannot undo this! These changes have never been saved to the repository, so Git has no way of restoring them. Please make sure that you really want to discard them!

Undo and Revert an Older Commit

Many times you only notice that you made a mistake much later after it has long been committed to the repository.

Revert Older Commit

The question then is: how can you get rid of this bad commit? The answer is: by using the git revert command.

$ git revert 2b504bee


When using this command, you might be surprised at the result - because the bad commit has not been deleted! Instead of deleting the commit, Git has automatically created a new commit with changes that revert the effects of our “bad” commit.


Revert Old Commit

A very elegant and low-risk way of undoing something!

Restore a Previous Version of a Project

Another scenario might be when you want to restore your complete project to an earlier revision. In this case, what you really want is to turn back time - and have your complete project exactly as it was at that certain point in time.

Below is a sample scenario, where we declare any commits that came after “C2” as unwanted. We would like to return to the state of revision “C2” and get rid of everything that followed:

Restore Previous Version of a Project

You already know the git reset command from our earlier adventures:

$ git reset --hard 2b504bee


We feed it the SHA–1 hash of the revision we’d like to return to. Commits C3 and C4 will then disappear from the project’s history.

When working with a Git desktop client like “Tower”, both the git revert and git reset commands are easy to reach from the contextual menu of a commit item:

git revert from within Tower

Restore Deleted Branches, Deal with Merge Conflicts, ...

It goes without saying that there are many more interesting and horrible ways to shoot yourself in the foot. But for most of these ways, Git very likely can help you clean up the mess.

If you want to learn more, you can have a look at our "First Aid Kit for Git" project. There is a nice little video course and a handy cheat sheet available – both for free!

Topics:
git ,version control ,open source ,git revert ,git commands ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}