How to use the command git restore (with examples)
Git is a version control system that allows developers to track changes and collaborate on projects. The git restore
command is used to restore working tree files to a previous state. This command is available in Git version 2.23 and above.
Use case 1: Restore an unstaged file to the version of the current commit (HEAD)
Code:
git restore path/to/file
Motivation:
Sometimes, after modifying a file, you realize that you want to revert it back to the state it was in the latest commit (HEAD). By using git restore
, you can easily discard the changes and restore the file to its previous state.
Explanation:
git restore
: The command used to restore files.path/to/file
: The path to the file you want to restore.
Example output:
Restored path/to/file
Use case 2: Restore an unstaged file to the version of a specific commit
Code:
git restore --source commit path/to/file
Motivation:
If you want to restore a file to the state it was in a specific commit, you can use the --source
flag followed by the commit hash. This can be useful when you need to retrieve the contents of a file from a previous commit.
Explanation:
--source commit
: Specifies the commit from which to restore the file.path/to/file
: The path to the file you want to restore.
Example output:
Restored path/to/file
Use case 3: Discard all unstaged changes to tracked files
Code:
git restore :/
Motivation:
When you have made changes to multiple files but haven’t staged them yet, you may want to discard all the changes and revert the files to their previous state. The command git restore :/
allows you to do this.
Explanation:
:/
: Specifies all files in the working tree.
Example output:
Restored path/to/file1
Restored path/to/file2
...
Use case 4: Unstage a file
Code:
git restore --staged path/to/file
Motivation:
If you have mistakenly staged a file and want to unstage it, you can use the --staged
flag with git restore
. This will remove the file from the staging area and keep it in its current state.
Explanation:
--staged
: Only applies the command to the staged changes.path/to/file
: The path to the file you want to unstage.
Example output:
Unstaged path/to/file
Use case 5: Unstage all files
Code:
git restore --staged :/
Motivation:
When you have staged multiple files and want to unstage all of them, you can use the git restore
command with the --staged
flag and :/
as the path. This will unstage all files and keep them in their current state.
Explanation:
--staged
: Only applies the command to the staged changes.:/
: Specifies all files in the working tree.
Example output:
Unstaged path/to/file1
Unstaged path/to/file2
...
Use case 6: Discard all changes to files, both staged and unstaged
Code:
git restore --worktree --staged :/
Motivation:
If you want to completely discard all changes to files, both staged and unstaged, you can use the --worktree
flag along with the --staged
flag and :/
as the path. This will revert all files to their previous state.
Explanation:
--worktree
: Discards changes in both the working tree and the staging area.--staged
: Only applies the command to the staged changes.:/
: Specifies all files in the working tree.
Example output:
Discarded changes in path/to/file1
Discarded changes in path/to/file2
...
Use case 7: Interactively select sections of files to restore
Code:
git restore --patch
Motivation:
The --patch
flag allows you to interactively select sections of files to restore. This can be useful when you only want to revert specific changes within a file and keep the rest.
Explanation:
--patch
: Interactively selects sections of files to restore.
Example output:
File: path/to/file
Diff:
@@ -2,6 +2,6 @@
public class Example {
public static void main(String[] args) {
- System.out.println("Hello, world!");
+ System.out.println("Hello, Git!");
}
}
Apply this change? [y]es/[n]o/[q]uit:
Conclusion:
The git restore
command provides an efficient way to restore working tree files to a previous state. Whether you want to revert changes to a specific commit, unstage files, or discard all changes, git restore
offers several use cases to help you manage your project effectively.