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:
- Go to the repository on GitHub
- Click the “Commits” tab
- Browse to the commit before the file was deleted
- Click the commit and browse the repository at that point
- Locate the file, click on it, and click “Raw”
- Copy the contents and manually restore it in your local repo
✅ Scenario 4: Use GitHub Desktop (Optional)
If you’re using GitHub Desktop:
- Open the repo in GitHub Desktop
- Go to History
- Find the commit that deleted the file
- 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
Scenario | Recovery Method |
---|---|
Deleted locally, not committed | git checkout -- file.txt or git restore file.txt |
Deleted in a past commit | git log → git checkout <hash>^ -- file.txt |
Deleted and pushed to GitHub | Restore via GitHub UI or revert commit |
Using GitHub Desktop | View history → restore file from older commit |