Data report"State of code review 2024" is now liveRead the full report

How to undo unstaged changes in Git

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


When working with Git, you may often find yourself in a situation where you need to undo changes that you've made but haven't yet staged for commit. This can be necessary for a variety of reasons, such as making a mistake in your code, experimenting with something that didn't work out, or simply deciding to discard changes to start fresh. Whatever the reason, Git provides several tools to handle this situation.

Unstaged changes are modifications in your working directory that have not been added to the staging area. Git allows you to see these changes by using the command:

Terminal
git status

This command will list all the files that have been changed, added, or deleted, but not yet staged.

Undo all unstaged changes

To undo all changes in your working directory, you can use the git checkout or git restore command. Since Git version 2.23, git restore is recommended as it's more explicit about what it does:

Terminal
git restore .

This command will restore your working directory to match the last commit, effectively undoing all unstaged changes.

Undo specific file changes

If you only want to undo changes in specific files, you can specify those files in the command:

Terminal
git restore file1.txt file2.txt

Replace file1.txt and file2.txt with the names of the files you wish to restore.

Git GUI

If you are using a graphical user interface (GUI) like Git GUI, you can undo changes by:

  1. Opening Git GUI and navigating to the changed files.
  2. Selecting the files you want to revert.
  3. Right-clicking and choosing "Revert Changes" from the context menu.

This action will discard the changes in the selected files.

Sometimes you might want to remove untracked files or directories (those that are new and not added to Git). To clean your repository of these:

Terminal
git clean -fd

This command removes untracked files (-f for "force") and directories (-d).

  • Safety: Always double-check which files you are reverting, especially in a shared repository. Undoing changes can't be easily reversed if those changes weren't committed or stashed.

  • Stashing: If you think you might want to revisit your changes later, consider using git stash to save the changes temporarily instead of discarding them:

    Terminal
    git stash push -m "Work in progress"

For more information see the official Git documentation.

Git gud
"It's the first Git workflow I've used that actually feels good."
–@robboclancy
Learn more

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2