How to Recover Deleted Files from GitHub

Git and GitHub maintain full version history, so you can recover deleted files in several ways—depending on where and how the file was deleted.


✅ Scenario 1: Recover a Deleted File Using Git (Local Repository)

If you deleted the file locally but haven’t committed yet:

git checkout -- path/to/your-file.txt

This restores the file from the last commit.


✅ Scenario 2: Recover a Deleted File from a Past Commit

If the file was deleted and committed, find the commit where it was still present:

Step 1: Use Git Log to Locate the File’s History

git log -- path/to/your-file.txt

This shows commits that affected the file.

Step 2: Restore the File from a Previous Commit

Copy the commit hash and run:

git checkout <commit-hash>^ -- path/to/your-file.txt

This checks out the file from before it was deleted. Then commit the restored file:

git add path/to/your-file.txt
git commit -m "Restore deleted file"

✅ Scenario 3: Recover from GitHub Web Interface

If the file was pushed and deleted on GitHub:

  1. Go to the repository on GitHub
  2. Click the “Commits” tab
  3. Browse to the commit before the file was deleted
  4. Click the commit and browse the repository at that point
  5. Locate the file, click on it, and click “Raw”
  6. Copy the contents and manually restore it in your local repo

✅ Scenario 4: Use GitHub Desktop (Optional)

If you’re using GitHub Desktop:

  1. Open the repo in GitHub Desktop
  2. Go to History
  3. Find the commit that deleted the file
  4. Right-click the previous commit and choose “Revert This Commit” or manually copy the file’s contents

🧠 Bonus: Use git restore (Modern Git)

If you’re using Git 2.23+:

git restore path/to/your-file.txt

Or from a specific commit:

git restore --source <commit-hash> path/to/your-file.txt

⚠️ Notes

  • If the deletion was part of a merge, recovering may require checking multiple branches or using git reflog
  • Always double-check your commit history before force pushing restored content

📋 Summary

ScenarioRecovery Method
Deleted locally, not committedgit checkout -- file.txt or git restore file.txt
Deleted in a past commitgit loggit checkout <hash>^ -- file.txt
Deleted and pushed to GitHubRestore via GitHub UI or revert commit
Using GitHub DesktopView history → restore file from older commit
Sharing Is Caring:

Leave a Comment