How to recover deleted file in Git

Recovering deleted files in Git is straightforward because Git tracks file history in commits even after deletion. With over 25 years of software development experience and as the creator of CoreUI, I’ve recovered numerous accidentally deleted files. Git provides multiple commands to restore files from commit history, whether deleted accidentally or through git operations. This approach helps you retrieve any file that was ever committed to the repository.

Use git checkout, git restore, or git reflog to recover deleted files from Git commit history.

Recover recently deleted file:

# You deleted a file and committed
git rm file.js
git commit -m "Remove file"

# Find when file was deleted
git log --oneline --all -- file.js

# Output shows:
# abc1234 Remove file
# def5678 Update file.js
# ghi9012 Add file.js

# Recover from commit before deletion
git checkout def5678 -- file.js

# Or use modern syntax
git restore --source=def5678 -- file.js

# Stage and commit the recovered file
git add file.js
git commit -m "Restore file.js"

Recover file deleted in working directory:

# You deleted file but haven't committed
rm file.js

# Restore from last commit
git checkout -- file.js

# Or with modern syntax
git restore file.js

# If file was staged
git restore --staged file.js
git restore file.js

Find when file was deleted:

# Search commit history for file deletion
git log --all --full-history -- path/to/file.js

# More detailed output
git log --all --full-history --oneline -- path/to/file.js

# Show actual deletion commit
git log --diff-filter=D --summary -- path/to/file.js

# Output shows:
# abc1234 Remove unused files
#  delete mode 100644 path/to/file.js

# Recover from commit before deletion
git checkout abc1234^ -- path/to/file.js

Recover multiple deleted files:

# Find all deleted files in a commit
git show --name-only --diff-filter=D abc1234

# Output:
# src/utils/helper.js
# src/config/settings.js
# tests/unit/test.js

# Recover all at once
git checkout abc1234^ -- src/utils/helper.js src/config/settings.js tests/unit/test.js

# Or recover entire directory
git checkout abc1234^ -- src/utils/

Recover file from specific commit:

# List commits that modified the file
git log --all --oneline -- file.js

# View file content at specific commit
git show abc1234:file.js

# Save to new file
git show abc1234:file.js > recovered-file.js

# Or restore directly
git checkout abc1234 -- file.js

Recover file using reflog:

# File was deleted but commit hash unknown
git reflog

# Look for commit mentioning the file
git reflog -- path/to/file.js

# Output shows:
# abc1234 HEAD@{5}: commit: Remove file.js
# def5678 HEAD@{6}: commit: Update file.js

# Recover from before deletion
git checkout HEAD@{6} -- path/to/file.js

Recover file from deleted branch:

# Branch with file was deleted
git branch -D feature-branch

# Find last commit of deleted branch
git reflog | grep feature-branch

# Output:
# abc1234 HEAD@{10}: commit: Add new feature file

# Recover file from that commit
git checkout abc1234 -- path/to/feature-file.js

Search file content in history:

# Find commits where file contained specific text
git log -S "function name" --source --all -- file.js

# Find commits by content with context
git log -p -S "function name" -- file.js

# Find by regex
git log -G "class.*Component" --all -- file.js

# When found, recover that version
git checkout <commit-hash> -- file.js

Recover file with full history:

# View complete history of file
git log --follow --all -- file.js

# Follow renames
git log --follow --oneline -- file.js

# If file was renamed, find original
git log --follow --diff-filter=R -- new-name.js

# Recover from any point in history
git checkout <commit-hash> -- file.js

Interactive file recovery:

# Show deletions with patches
git log --diff-filter=D --patch -- "*.js"

# Use git log with interactive mode
git log --all --full-history --oneline --name-only -- deleted-file.js

# View each version interactively
git log -p --all -- file.js | less

Recover files deleted in merge:

# File deleted during merge
git log --merge --all -- file.js

# Find pre-merge version
git log --oneline --all -- file.js

# Recover from before merge
git checkout <commit-before-merge> -- file.js

# Or check both merge parents
git show HEAD^1:file.js  # First parent
git show HEAD^2:file.js  # Second parent

Recover from stash:

# File might be in stash
git stash list

# Check each stash
git stash show stash@{0} --name-only

# If file is there, restore it
git checkout stash@{0} -- file.js

# Or apply entire stash
git stash apply stash@{0}

Compare file versions:

# Compare deleted version with current
git diff abc1234:file.js def5678:file.js

# View differences before recovering
git diff HEAD abc1234 -- file.js

# Show what changed when file was deleted
git show abc1234 -- file.js

Recover entire directory:

# Recover directory and all files
git checkout abc1234 -- path/to/directory/

# List all files in directory at commit
git ls-tree -r abc1234 -- path/to/directory/

# Recover specific pattern
git checkout abc1234 -- "*.js"
git checkout abc1234 -- "**/*.css"

Prevent file loss:

# Check what will be deleted before committing
git status
git diff --name-only

# Use interactive staging
git add -p

# Create backup branch before dangerous operations
git branch backup-before-cleanup

# Soft delete (keep in working directory)
git rm --cached file.js

Recover from bare repository:

# In bare repository, list file at commit
git show <commit>:path/to/file.js

# Extract file
git show <commit>:path/to/file.js > /tmp/recovered-file.js

# Clone to recover
git clone /path/to/bare-repo.git temp-recovery
cd temp-recovery
git checkout <commit> -- path/to/file.js

Automate file recovery:

# Script to recover multiple files
#!/bin/bash
FILES=(
  "src/components/Button.js"
  "src/utils/helper.js"
  "tests/unit.test.js"
)

COMMIT="abc1234"

for file in "${FILES[@]}"; do
  echo "Recovering $file..."
  git checkout $COMMIT -- "$file"
done

git status

Best Practice Note

Use git log --all --full-history -- filename to find when a file was deleted. Recover with git checkout <commit>^ -- filename using the commit hash before deletion. Modern Git uses git restore --source=<commit> instead of checkout. Always verify file content with git show before recovering. Use --follow flag to track files through renames. Files are only recoverable if they were committed—unstaged changes cannot be recovered after deletion. This is how we recover deleted files in CoreUI development—checking commit history, verifying content before recovery, and using proper Git commands to restore accidentally deleted work without data loss.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author