“git restore” Command Examples

The git restore command is a powerful addition to Git version 2.23 and later that provides a more intuitive way to interact with your working tree (your files on disk) by selectively restoring or discarding changes. It simplifies certain tasks that were previously performed using various combinations of git checkout and git reset.

Here’s a deeper look into the git restore command:

1. Working Tree Restoration: The primary purpose of git restore is to restore files in your working directory. This can mean different things depending on the situation:

  • Discarding Unstaged Changes: If you have changes in your working directory that haven’t been staged (added to the index), you can use git restore to undo those changes and revert the file to the state it was in when you last committed.
  • Unmodifying Staged Files: If you’ve staged changes but want to revert those changes before committing, git restore helps you “unmodify” those files, effectively taking them back to the state they were in before staging.

2. Selective Restoration: With git restore, you can target specific files or directories for restoration, allowing you to restore only the changes you need while leaving other files untouched.

3. File Modes and Permissions: git restore respects file modes and permissions, ensuring that restored files maintain their original executable flags, if applicable.

4. Simplified Syntax: The syntax of git restore is more straightforward compared to some alternatives. It accepts a file path as an argument and restores the file based on the state of the index or the HEAD commit, depending on the options used.

5. Integration with Staging: You can use git restore to unstage files that were added to the staging area using git add, effectively moving them from the staged area back to the working directory.

6. Options for Working with Index and HEAD: “git restore” provides options to restore based on different references:

  • –staged or –source: Restore files from the index (staging area) to the working directory.
  • –worktree: Restore files from the HEAD commit to the working directory.

7. Caution with Uncommitted Changes: It’s important to note that git restore can discard uncommitted changes in your working directory. Be cautious when using it to ensure you don’t unintentionally lose important work.

The introduction of git restore simplifies and clarifies the process of managing changes in your working directory and staging area. It provides a more intuitive way to handle file-level changes and is particularly useful for those scenarios where the old syntax of git checkout and git reset could lead to confusion.

git restore Command Examples

1. Restore an unstaged file to the version of the current commit (HEAD):

# git restore path/to/file

2. Restore an unstaged file to the version of a specific commit:

# git restore --source commit path/to/file

3. Discard all unstaged changes to tracked files:

# git restore :/

4. Unstage a file:

# git restore --staged path/to/file

5. Unstage all files:

# git restore --staged :/

6. Discard all changes to files, both staged and unstaged:

# git restore --worktree --staged :/

7. Interactively select sections of files to restore:

# git restore --patch
Related Post