git restore Command


git restore Command

The git restore command is used to restore files in your working directory or staging area to their state in a specific commit, or to undo changes. It is part of Git’s newer set of commands designed to simplify and clarify common operations.


What Does git restore Do?

The git restore command allows you to:

  1. Undo Changes in the Working Directory: Restore files to their state in the latest commit or a specific commit.
  2. Unstage Changes: Remove changes from the staging area, effectively undoing git add.

Basic Syntax

Restore Files to Their Last Committed State

git restore <file>
  • <file>: The path to the file you want to restore.

This command will discard changes made to <file> in your working directory, restoring it to the state of the last commit.

Restore Files to a Specific Commit

git restore --source <commit> <file>
  • <commit>: The commit hash or reference to restore the file from.
  • <file>: The path to the file you want to restore.

This command will restore the specified file to its state in the given commit.

Restore the Staging Area

git restore --staged <file>

This command will remove changes from the staging area, effectively undoing git add for the specified file. The file will still be modified in your working directory.

Restore Both Working Directory and Staging Area

To restore a file to its state in the last commit and remove it from the staging area:

git restore --source=HEAD --staged <file>
  • --source=HEAD: Specifies that the file should be restored to its state in the current commit (HEAD).
  • --staged: Removes the file from the staging area.

Restore All Files

To restore all files to their state in the latest commit:

git restore .

This will discard all changes in the working directory, restoring all files to their last committed state.


Examples

Undo Local Changes

To discard local changes made to file.txt and restore it to the state in the latest commit:

git restore file.txt

Unstage a File

To unstage file.txt after it has been added to the staging area:

git restore --staged file.txt

Restore a File from a Specific Commit

To restore file.txt to its state in commit abc1234:

git restore --source abc1234 file.txt

Restore All Files in the Repository

To discard all changes in the working directory and restore all files to their state in the latest commit:

git restore .

Important Notes

  • git restore vs. git checkout: Prior to git restore, you would use git checkout for similar tasks. git restore is intended to be more intuitive for restoring files and is part of Git’s efforts to simplify commands and their purposes.
  • Data Loss Warning: Using git restore will discard changes in your working directory. Ensure you do not have uncommitted work you wish to keep before running these commands.

Summary

  • Purpose: git restore is used to restore files to their state in a specific commit, undo local changes, or unstage files.
  • Basic Syntax:
    • git restore <file>: Restore file to last committed state.
    • git restore --source <commit> <file>: Restore file to state in a specific commit.
    • git restore --staged <file>: Unstage file.
    • git restore .: Restore all files to last committed state.

The git restore command helps you manage changes in your repository more effectively by providing a clear and straightforward way to revert files and undo staging operations.