Git commands

git restore

git restore: restore the working tree and/or staging area

The git restore command is used to restore files in the working tree from either the index or another commit (the --worktree mode, which is default). It can also restore the index itself from the HEAD or another commit (the --staged mode).

This command was introduced in Git version 2.23.0 as a cleaner alternative to using git reset for unstaging changes and discarding local modifications. The primary purpose of git restore is to help developers manage their working directory by providing a simple way to undo changes that haven't been committed yet. This is particularly useful when you've made some changes that you're not happy with and want to revert to a previous state.

Use cases

  1. Restore working tree (nothing is staged): Suppose you started with a clean working tree, began working on something, but eventually realized that the new changes are not what you wanted, and you'd rather start from scratch. In Git terms, this means that you want to restore the working tree from the HEAD (latest commit).

    Solution: git restore --worktree . OR simply git restore ., because the --worktree mode is default.

  2. Restore staging area, keep working tree: Same situation, but this time you were happy with the changes. However, this was a big change, so you decide to commit it in several smaller commits. You need to stage a few files here, make a commit, stage a few files there, make a commit, and so on. But as you start, your clumsy fingers stage the whole project instead. So, now you want to restore just the index from the HEAD (but leave all the current changes intact).

    Solution: git restore --staged .

  3. Restore working tree, keep staging area: Same situation, but you managed to stage the changes correctly. Just before committing, you spotted one last bug, which after 5 minutes of fixing, turned out to be... not a bug. So, you want to roll back everything you changed since staging, and return to the happy place you were in when you were ready to commit. In Git terms, this means that you want to restore the working tree from the index.

    Solution: git restore --worktree . OR git restore .

  4. Restore staging area and working tree: Same situation as above, but you realized that your changes are not worth committing, and want to restore your files and clean up the index, so that your working tree is totally clean. In Git terms, you want to restore both the working tree and the index from the HEAD.

    Solution: git restore --worktree --staged .

Examples

Discard changes to a specific file in the working directory:

git restore README.md

Discard changes to all files in the working directory. The . character points to the current directory, meaning "all changes in the current directory":

git restore .

Unstage changes in a specific file from the staging area (but keep the actual changes in the working directory):

git restore --staged README.md

Unstage all changes and restore the working tree from the state of the latest commit:

git restore --worktree --staged .

Restore the working tree to the state that matches a specific commit using the --source option set to a1b2c3d4.

If --source is omitted, the contents are restored from HEAD if --staged is given, otherwise from the index (if nothing is staged, it's effectively the same as HEAD).

git restore --source a1b2c3d4 README.md

Cycle through all the changes and choose what to discard interactively:

git restore --patch

Or the shorter form:

git restore -p
© 2024-2025 GitByBit.All rights reserved.

Hi! I'm Alex, creator of GitByBit.

And this is Gitopedia—a reference that summarizes knowledge in my Git course.

Gitopedia is designed to be a personal knowledge base. When you progress through the main Git course in VS Code, you unlock all of these topics, one by one, bit by bit. This gives you time to digest the information and gradually build the mental model of Git. Here's an example of how it works:

Learning directly in VS Code lets you practice Git exactly as you would in real life, doing real jobs, writing real code. In addition, the course has access to your actual terminal, so it can point out mistakes, suggest workarounds, etc.

The course is FREE, there are no Ads or other bullshit. There are optional premium add-ons you can purchase, mainly to support my work (regrettably, I have to eat every day), but that's totally up to you.

Learn Git in VS Code