Undoing Things

How can I undo things in Git?

Transcript
Undoing Things

In this video, I’ll show you how to undo things with Git. Git allows you to undo almost anything. Knowing this should let you sleep a bit easier at night.

Let’s start by changing your very last commit. Often after making a commit, you realize that you forgot to add a little change. Or you mistyped the commit message.

I’ll stage "file-A" and "file-B" and commit them.

As I said, the most recent commit can easily be changed in Git. I can correct the commit message, simply by “committing again”, with the “amend” option. As you see, the latest commit was simply replaced by our amended, corrected one.

You can also add some more changes to that last commit. Let’s say we forgot to add "file-C". I simply stage it as normal and then commit again.

The amend feature is great for fixing your very last commit - as long as you keep one rule in mind: never amend a commit that was already published on a remote server. Amend rewrites a commit; and if you publish that commit again, you’ll come into trouble.

Tower also allows you to undo uncommitted local changes.

Sometimes you realize that the changes you just made… well… aren’t worth keeping. You’d like to restore the last committed state of a file and start fresh.

Let’s do this with our “index.html” file. Simply right-click the file and select “Discard Local Changes”.

This discards any uncommitted changes we’ve made in that file since we last committed it. You see that it’s not listed as modified anymore.

In case you want to discard only some changes in a file, Tower allows you to do this, too. Select the file in the working copy and have a look at its diff on the right side. You can either discard a complete chunk of changes or even select individual lines that you want to discard.

If you had a very bad day, you might want to discard all of the changes you made recently. Right-click the Working Copy item in the sidebar and choose “Discard All Local Changes”. This discards all local changes and restores the complete project at its last committed state.

Be very careful with these two commands - because you cannot restore uncommitted changes after you’ve discarded them.

Finally, you can also undo commits. E.g. when you notice that your changes were wrong or when you introduced a bug.

Using the “revert” command is one possibility to undo a commit. However, the command doesn’t delete any commits. In fact, it even creates a new commit. One that reverts the effects of that other commit, effectively undoing it.

For example, if your original commit added a word in a certain place, the reverting commit will remove exactly this word, again.

To do this in Tower, let’s select the commit history of our HEAD branch. Now, let’s say we introduced some bad code with this “Change headlines” commit here.

I changed the “About” text to “About this Project”; and “Imprint” to “Imprint Slash Disclaimer”. Let’s revert this commit. Simply right-click it and select the revert option.

Now, we indeed have a new commit that reverted the old one. And, with a closer look, we see that it indeed introduced changes that revert that other commit.

A different way to “undo” commits is the “reset” command. It works by resetting your HEAD branch to an older revision (also called “rolling back” to that older revision). After this command, your currently checked out branch will be at the named revision. And the files in your working copy will be exactly as they were in that revision.

All the commits that came after this one are effectively undone. They are no longer visible in the history of this branch.

Let’s say we want to roll back our working copy to the “Change headlines” commit here. Right-click it and select the reset option. Now, that commit is the latest one and any newer commits are effectively undone.

One last thing to keep in mind: Both commands, revert and reset, only affect your current HEAD branch. Therefore, always make sure you have both checked out the correct branch and selected it in the sidebar.

That’s it for now. Thanks for watching - and see you in our next video!