AlgoMaster Logo

git restore

Last Updated: January 3, 2026

6 min read

If you've ever found yourself in a situation where you made changes to a file in your Git repository and wanted to go back to a previous state without disrupting your commit history, you're not alone.

The git restore command is a powerful tool designed for undoing changes in a more granular way compared to other commands. It allows you to selectively discard changes in your working directory or staging area, providing a safety net for your workflows.

What is git restore?

The git restore command was introduced in Git 2.23 as part of a larger effort to simplify the command-line interface for users. Its primary purpose is to restore files in the working directory or the staging area. While it may seem similar to git checkout, git restore is more focused and user-friendly for undoing changes.

When you use git restore, you're interacting specifically with the contents of your files, either to discard changes you've made or to bring back a version from the repository. This command acts on the following three primary areas:

  • Working directory: The files you're actively editing.
  • Staging area (index): The files that are ready to be committed.
  • Repository: The historical commits where you can pull previous versions of files.

Discarding Changes in the Working Directory

One of the most common scenarios for using git restore is to discard uncommitted changes in your working directory. This is particularly useful when you’ve made modifications to a file and realize you want to revert those changes.

Basic Usage

To discard changes in a file, you can run:

This command will reset the specified file in your working directory to the state of the last commit. For example, if you modified example.txt but decide you want to discard those changes:

After executing this command, example.txt will go back to the last committed state, losing all uncommitted changes.

Discarding Changes in Multiple Files

You can also restore multiple files at once:

This command is convenient when you have several files with unwanted changes.

Restoring Staged Changes

Sometimes, you may stage changes but realize you want to modify them before committing. In this case, git restore allows you to unstage files.

Unstaging Files

To unstage a file, use the --staged option:

For example, if you staged example.txt but want to make further changes before committing, you would run:

This command moves the changes from the staging area back to the working directory. The file now reflects the changes you made, allowing you to edit it further.

Unstaging Multiple Files

You can also unstage multiple files at once:

This is useful for cases where you need to adjust several files before committing them.

Restoring to a Specific Commit

In addition to restoring files to their last committed state, git restore allows you to pull versions of a file from any specific commit. This can be extremely useful for recovering an earlier version of a file without affecting your current commit history.

Restoring from a Commit

To restore a file from a specific commit, use the following syntax:

For instance, if you want to restore example.txt from a commit with the hash abc1234, you would execute:

This command takes the version of example.txt from the specified commit and replaces the current version in your working directory.

Visualizing Changes

To know which commit you want to restore from, you can use:

This command will show you a simple graph of recent commits, helping you identify the correct commit hash.

Combining with Other Options

While git restore is powerful on its own, combining it with other options can enhance its functionality. Here are some useful combinations:

Restoring with Checkout Options

If you want to restore a file and also check it out at the same time, you can combine git restore with --worktree:

This command ensures that the specified file in your working directory reflects the state from the specified commit.

Using Paths

If you want to restore all files in a directory or specific patterns, you can use paths:

This command will restore all files in the specified directory from a specific commit.

Real-World Use Cases

Understanding practical scenarios where git restore shines can help solidify its importance in your workflow. Here are some examples:

Mistaken Edits

Imagine you’re working on a feature and accidentally modify a few lines in a file. You realize the changes are unnecessary and now wish to discard them. Instead of going through each change manually, you can simply run:

Pre-Commit Adjustments

Suppose you've added several files to the staging area but want to make last-minute adjustments before committing. You can unstage them using:

This gives you the chance to refine your changes, ensuring only the best code goes into your commit.

Experimentation with Version History

If you are exploring how a file has evolved over time, you can restore different versions to see how changes impacted the codebase. For example:

This practice can provide insights into why certain decisions were made in the code, helping you make informed choices in your current work.

Summary

The git restore command is a versatile tool that simplifies the process of discarding unwanted changes and managing your staging area. By allowing you to restore files from the working directory or the repository's history, it empowers you to handle changes more confidently and efficiently.

Using git restore, you can:

  • Discard changes in your working directory.
  • Unstage files that are ready for commit.
  • Restore specific file versions from past commits.

Mastering git restore equips you with the capabilities to refine your workflow and ensures you maintain a clean commit history.

Now that you understand how to effectively use git restore to manage your changes, you are ready to explore git reset. In the next chapter, we will look at how reset can help you manipulate your commit history and manage your staging area even further. Get ready to uncover the intricacies of resetting your repository!