Git is a powerful version control system widely used in software development to manage changes to source code. One of its useful commands is git restore
, which allows you to restore files in your repository to a previous state. This can be crucial when you need to recover accidentally deleted files or revert changes that introduced errors.
Let’s explore several examples of using git restore
in different scenarios:
Example 1: Restoring a single file to its previous state:
git restore --source=HEAD~1 path/to/file.txt
This command retrieves the version of file.txt
from one commit before the current HEAD
(typically the last commit).
Verification: Check the content of file.txt
to confirm it matches the state before the last commit.
Example 2: Discarding changes made to a file since the last commit:
git restore --staged path/to/file.js
Here, git restore
unstages any changes made to file.js
, effectively reverting it to the state it was in when it was last committed.
Verification: Verify the status of file.js
using git status
to ensure it is no longer staged for commit.
Example 3: Restoring a deleted file:
git restore --source=HEAD^ --
This command retrieves the contents of a file that was previously deleted in the most recent commit.
Verification: List files in the directory to see if deleted_file_path
has been restored.
Example 4: Restoring all files to match the state of the last commit:
git restore --source=HEAD -- .
This command resets all files in the current directory to match the state of the last commit on the current branch.
Verification: Use git diff
to check if there are any differences between the working directory and the last commit.
Example 5: Restoring a file to its state from a specific commit:
git restore --source= --
Replace <commit_hash>
with the actual commit hash and <file_path>
with the path to the file. This restores the file to how it appeared at the specified commit.
Verification: Review the contents of <file_path>
to ensure it reflects the state from <commit_hash>
.
Example 6: Restoring a file to its state before specific changes:
git restore -p path/to/file.py
Using the -p
flag prompts you to interactively choose which parts of the file to restore from previous versions.
Verification: Inspect file.py
to see if the changes you intended to revert are no longer present.
Example 7: Restoring file permissions:
git restore --source=HEAD --staged --worktree -- path/to/file.sh
This command restores the permissions of file.sh
to match those in the last commit across the staging area, the working directory, and the repository.
Verification: Use ls -l path/to/file.sh
to check the permissions before and after restoration.
Example 8: Restoring a file from a remote branch:
git restore --source=origin/main -- path/to/file.txt
Replace origin/main
with the remote branch name. This restores file.txt
to its state as it exists in the specified remote branch.
Verification: Compare the contents of file.txt
to those in the remote branch using git diff
.
Example 9: Restoring a file using a specific strategy:
git restore --source=HEAD --strategy=theirs path/to/conflicted_file.txt
Here, --strategy=theirs
resolves conflicts in favor of changes from the branch being merged. This restores conflicted_file.txt
with the chosen strategy.
Verification: Check the resolved state of conflicted_file.txt
to ensure the desired changes have been applied.
Example 10: Restoring file attributes:
git restore --source=HEAD --source=HEAD --attributes path/to/file.txt
This command restores the attributes of file.txt
to match those in the last commit.
Verification: Use git ls-tree HEAD path/to/file.txt
to compare the attributes before and after restoration.
Discussion about this post