How to discard 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.


This guide will cover several methods to discard changes in Git, using various commands to handle different scenarios. We'll provide examples for each situation to help you understand how to revert your work to a desired state.

Before diving into the commands, let's clarify the main areas of Git:

  1. Working directory: The Git working directory, also known as the working area, is the space on your local filesystem where the actual files and directories of your project are stored and modified before being committed to the remote repository.
  2. Staging area (index): This is where you prepare and organize changes before committing them to the remote repository. It acts as a middle ground between your local machine and the repository stored on a remote server.
  3. Commits: These are records of your past code changes, which are stored in the project history.

The git checkout command is one of the most common ways to revert changes:

  • Discard changes in the working directory: To revert a specific file to the last committed state, use:

    Terminal
    git checkout <file>

    Replace <file> with the name of the file you want to revert.

  • Discard all changes in the working directory: If you want to discard changes from all files in the working directory:

    Terminal
    git checkout .

This will revert all of your tracked files back to the state they were in at the last commit. However, this will not affect "untracked" files, new files that have been created locally but not committed yet, or other files that are auto-generated and shouldn't be committed to the repository like build artifacts, compiled binaries, log files etc.

Git 2.23 introduced a more straightforward command called git restore, designed to make it easier to work with changes:

  • Discard changes to a specific file:

    Terminal
    git restore <file>
  • Discard changes to all files in the working directory:

    Terminal
    git restore .

This command is versatile and can also be used to restore files from a specific branch or commit.

For files that are not tracked by Git (new files or automatically generated files that are not in your .gitignore), you can use the git clean command to clean your working directory:

  • Discard untracked files:

    Terminal
    git clean -f
  • Discard untracked directories:

    Terminal
    git clean -fd

This command will remove all files and directories that are not under version control, so use it with caution as these changes cannot be undone.

Both of the above commands will leave files explicitly ignored by Git (i.e. files listed in the .gitignore file) intact. To delete ignored files in addition to all of the other untracked files, you can add the -x flag:

Terminal
git clean -fx

If you need to discard all staged and unstaged changes, you can use git reset:

  • Discard all changes (staged and unstaged):

    Terminal
    git reset --hard

This command resets your working directory and staging area to match the last commit. It's a powerful command that should be used carefully, as it will permanently erase all changes. However, untracked files will be untouched by this command.

Here are a few more specific use cases and how to handle them:

  • Discard changes in one file that are staged:

    If you have added changes to the staging area (with git add) and want to remove them from there, you can use:

    Terminal
    git restore --staged <file>

    If you also want to discard these changes in the working directory, after restoring the file run:

    Terminal
    git checkout <file>

For further reading see the official Git documentation.

On this page
Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
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