Undoing

Author

Marie-Hélène Burle

This section covers a few of the ways actions can be undone in Git.

Amending the last commit

Here is a common scenario: you make a commit, then realize that you forgot to include some changes in that commit; or you aren’t happy with the commit message; or both. You can edit your latest commit with the --amend flag:

git commit --amend

This will hide your last commit (as if it had never happened), add the changes in the staging area (if any) to the changes in that last commit, open a text editor showing the message of the last commit (you can keep or edit that message), and create a new commit which replaces your last commit.

So if you only want to change the commit message, run that command with an empty staging area. If you want to add changes to the last commit, stage them, then run the command.

In short, what this does is to replace your last commit with a new commit with the added changes and/or edited message. This prevents having a messy history with commits of the type “add missing file to last commit” or “better message for last commit: bla bla bla”. If you made a typo in your last commit message (and if you care about having a nice, clean history), you can fix it easily this way.

Your turn:

  • Run git log --oneline (notice the hash of the last commit)
  • Edit your last commit message
  • Run git log --oneline again to see that your last commit now has a new hash (so it is a different commit) and a new message
  • Now, make some change in your project (add a file, or edit a file… any change you want)
  • Then add that new change to your last commit without changing the message

Unstaging

You know how to add changes to the staging area. But what if you want to unstage changes? You don’t want to loose those changes. But you staged them and then realized that you don’t want to include them in your next commit after all.

Here is the command for this:

git restore --staged <file>

Note that Git will remind you about the existence of this command when you run git status and have staged files ready to be committed.

Your turn:

  • Make changes to one of your existing files
  • Stage that file
  • Run git status and notice Git’s reminder about this command
  • Unstage the changes on that file

Erasing modifications

Now, what if you made changes to a file, then decide that they were no good? You can easily get rid of these edits and restore the file to its last committed version:

git restore <file>

Note that Git will tell you about this command when you run git status and have unstaged changes in tracked files.

Your turn:

  • Run git status again and notice Git’s reminder about the existence of this command
  • Erase that last change of yours
  • Open your file and notice that your edits are gone

As you just experienced, this command leads to data loss.
Those last edits are gone and unrecoverable. Be very careful when using this!

Reverting

The working directory must be clean before you can use git revert.

git revert creates a new commit which reverses the effect of past commit(s).

To revert the last commit (current location of HEAD):

git revert HEAD

You can use the hash of the last commit instead of HEAD.

To revert the last two commits:

git revert HEAD~

HEAD~ is equivalent to HEAD~1 and means the commit before the one HEAD is on.

Here too of course, you can use the hash of the commit before last instead of HEAD~.

To revert the last three commits:

git revert HEAD~2