Skip to content

Home

Restore deleted files in a Git commit

Have you accidentally deleted a file? Or maybe you need to restore a file that was deleted? Luckily, Git provides a way to restore files that were deleted in a specific commit.

All you need is git checkout <commit>^ -- <pathspec>. This command will restore the specified <file> that was deleted in the specified <commit>. As the latter part of the command accepts a filename or fileglob, you can even restore multiple files.

# Syntax: git checkout <commit>^ -- <pathspec>

# "30seconds.txt" was deleted in the commit `3050fc0de`
git checkout 3050fc0de^ -- "30seconds.txt"
# Restores the 30seconds.txt file

# "src/*.json" was deleted in the commit `3050fc0de`
git checkout 3050fc0de^ -- "src/*.json"
# Restores all JSON files in the src/ directory

More like this

Start typing a keyphrase to see matching snippets.