Complete Git Restore Command Reference
Master the art of restoring files in Git
Git restore is a versatile command for restoring working tree files. This comprehensive guide covers all essential git restore commands with practical examples and safety information.
(--worktree)
(--staged)
(--source)
Basic Restore Commands
Revert file to last committed state. Discards unstaged changes in the working directory.
$ git status
modified: file.txt
$ git restore file.txt
$ git status
nothing to commit, working tree clean
Revert all files in current directory to last committed state. Discards all unstaged changes.
$ git status
modified: file1.txt, file2.txt, subdir/file3.txt
$ git restore .
$ git status
nothing to commit, working tree clean
Remove file from staging area while keeping changes in working directory. Effectively "unstages" the file.
$ git add file.txt
Changes to be committed: file.txt
$ git restore --staged file.txt
$ git status
Changes not staged for commit: file.txt
Unstage all files while keeping changes in working directory. Useful when you've added too many files.
$ git add .
Changes to be committed: file1.txt, file2.txt
$ git restore --staged .
$ git status
Changes not staged for commit: file1.txt, file2.txt
Advanced Restore Commands
Get version from 2 commits ago. Restores the file to how it was two commits back.
$ git log --oneline
abc1234 Current commit
def5678 Previous commit
ghi9012 Two commits ago
$ git restore --source=HEAD~2 file.txt
# file.txt now matches how it was at ghi9012
Restore a deleted file from a specific commit. The file will be added back to your working directory.
$ git log --oneline -- deleted_file.txt
abc1234 Last commit where file existed
$ git restore --source=abc123 deleted_file.txt
# deleted_file.txt is now restored
Interactive patch restoration. Allows you to selectively discard changes in a file.
$ git restore -p file.txt
Discard this hunk from worktree [y,n,q,a,d,e,?]?
# y - yes, discard this hunk
# n - no, don't discard this hunk
# q - quit
# a - discard all remaining hunks
# d - don't discard any remaining hunks
# e - manually edit the hunk
Restore file to match its state in another branch. Useful for bringing changes from feature branches.
$ git branch
* main
feature-branch
$ git restore --source=feature-branch file.txt
# file.txt now matches the version in feature-branch
Reset both staging and working copy. Completely reverts the file to HEAD state.
$ git status
Changes to be committed: file.txt (modified)
Changes not staged for commit: file.txt (modified)
$ git restore --worktree --staged file.txt
$ git status
nothing to commit, working tree clean
Restore file while ignoring unmerged entries. Useful when dealing with merge conflicts.
$ git status
Both modified: file.txt
$ git restore --ignore-unmerged file.txt
# Restores file.txt while ignoring unmerged paths
Remember that git restore is a relatively new command (introduced in Git 2.23)
that provides a more intuitive way to restore files compared to the older
git checkout and git reset commands for file operations.