Skip to content

6. Undoing Changes & Resetting History

Discarding local changes (git restore, git checkout)

Section titled “Discarding local changes (git restore, git checkout)”

There will be times when you need to reset your working directory to a clean state. Whether it’s due to mistakes or a need to abandon changes, Git provides several commands to help you manage this cleanup.

One of the primary commands you’ll use is git restore. Introduced in Git 2.23, it split the file-restoring job out of the overloaded git checkout into a clear, purpose-built command, and it can act on both your working directory and your staging area. Think of it as the modern, less error-prone way to do what people used to do with git checkout <file> and git reset <file>.

To discard changes in your working directory, you can run:

Terminal window
git restore .

This command will revert all modified and deleted files in the working directory to their last committed state. However, it will not affect the staging area, the zone where changes are prepared for committing.

If you only want to unstage files (i.e., move them from the staging area back to the working directory), use:

Terminal window
git restore --staged <file>

Or if you need to reset the whole directory:

Terminal window
git restore --staged .

To restore all files in both the staging area and the working directory to their state in the last commit, run:

Terminal window
git restore --source=HEAD --staged --worktree .

You can choose to restore a file that you previously deleted while still needing it:

Terminal window
git restore --source=HEAD <deleted-file>

Here HEAD is just a pointer to the latest commit on the active branch, so --source=HEAD means “bring this file back to how it looked in the last commit.”

The best way in the latest versions of Git to unstage files is to use the git restore --staged . command.

However, there is also the older command reset. This one can be quite confusing because it’s not always explicit about what is being reset.

To reset a staged file you can run:

Terminal window
git reset <file>

If you need to reset all staged files you can run:

Terminal window
git reset

This will unstage all your files but your changes will still be there (you will not lose your code).

When you need to undo a recent commit, there are different options available depending on whether you want to keep or discard the changes. Press the buttons below to compare them side by side:

HEAD points at C4, the latest commit. Your working directory is clean.

Working directory / staging: clean

In most cases, you’ll want to reset your recent commit to either amend the commit message or because the content wasn’t ready, but still keep your staged files and local changes. In other words, you want to reset only the commit history.

To do this, you can use the following command:

Terminal window
git reset --soft HEAD~

This command is elegant because it moves your HEAD back by one commit (i.e., resetting the erroneous commit you just made) while keeping your changes intact. Your working directory and staging area remain as they were, so you can modify the files or update the commit message before making a new commit. The ~ here is shorthand for ~1 (one commit back); HEAD~2 would move back two commits, and so on.

If you need to reset your latest commit and discard all changes made since then (for example, to undo an erroneous merge), you can use:

Terminal window
git reset --hard HEAD~

This will move your HEAD back by one commit and completely discard all changes in both the working directory and the staging area since that commit.

Sometimes, you don’t need to reset an entire commit just to fix a mistake. When you want to change a commit message or add a missing file, there’s no need to reset the entire commit history. Git provides a command for this called --amend.

If you run:

Terminal window
git commit --amend

This will allow you to edit your latest commit message. For example, you can fix a typo or adjust the commit message to be more accurate.

If you need to add or remove a file from the commit, follow these steps:

  1. Add the missing file:
Terminal window
git add <file>
  1. Or, remove a file:
Terminal window
git rm <file>
  1. Then, run the following to amend the commit without changing the message:
Terminal window
git commit --amend --no-edit

The --no-edit flag tells Git that you don’t want to modify the commit message. Instead, it will update the commit to include the added or removed files.

The --amend command effectively rewrites the latest commit. It replaces the old commit in the history with the newly amended one. This means the original commit will no longer be visible after you amend it.

Not all files in a project should be pushed to a remote repository. Some, like .env files and credentials, should always remain on your local machine. However, mistakes happen, and you might accidentally commit and push sensitive or unwanted files. In such cases, you need to revert the commit to remove the changes while keeping your commit history intact.

The advantage of using git revert is that it’s a non-destructive action. Instead of deleting or modifying the commit history, it creates a new commit that undoes the changes from the previous commit. This approach is recommended because rewriting history can cause issues for other contributors working on the project.

As a general rule, Git encourages avoiding history rewrites for commits that have already been shared with others. This same principle applies when we discuss rebasing in Chapter 8.

To revert a commit, use:

Terminal window
git revert <commit-hash>

This command generates a new commit that negates the changes introduced by the specified commit. Once done, you can push the reverted commit to the remote repository so everyone stays up to date.

Reverting a merge commit needs one extra detail. A merge has two parents, so you must tell Git which one to treat as the mainline with -m (almost always -m 1, the branch you merged into):

Terminal window
git revert -m 1 <merge-commit-hash>

Git provides multiple ways to undo changes, and at first they can feel interchangeable. They are not. The key question is always: what exactly am I trying to undo, and has it been shared with others yet?

Let’s line up the three commands people most often confuse:

  • git checkout / git restore works on files. It discards uncommitted changes in your working directory and brings a file back to a previous state. It does not touch your commit history.
  • git reset works on the branch pointer. It moves your branch back to an earlier commit, optionally throwing away the changes in between. With --hard this rewrites history and is destructive.
  • git revert works by adding history. Instead of erasing a commit, it creates a brand new commit that undoes the effect of an earlier one. Nothing is rewritten, which makes it safe to share.
Featuregit checkout / git restoregit reset --hardgit revert
Operates onFiles in the working directoryThe branch pointer (commit history)The branch, by adding a new commit
EffectDiscards local file changesMoves the branch pointer back and erases commitsCreates a new commit that undoes changes
History rewriteNo (history untouched)Yes (destructive)No (safe)
Use caseDrop edits you haven’t committedUndo local commits before pushingRevert changes safely in a shared repository
Impact on remoteNoneForce push needed only if the commits were already pushedDoes not require force push
  • Use git restore (or git checkout <file>) when you just want to throw away uncommitted edits to a file.
  • Use git reset --hard only when you haven’t shared your changes yet (e.g., fixing local commits before they leave your machine).
  • Use git revert when you need to undo a commit while keeping history intact. This is the safer option, especially when working in a team.

By understanding these differences, you can choose the right approach based on your situation and avoid unnecessary issues in collaboration.