It happens to almost every developer. You delete a file in your project, then you realise a second later that you still need it. If you have not committed the deletion yet, there is very good news.
Git tracks your files carefully, and in most cases you can bring a deleted file back with a single command. You do not need backup software or a special recovery tool.
This guide explains how recovery works, which command to use for your exact situation, and how to avoid losing files in the future.
Quick Answer
If you deleted a file but have not committed the deletion, restore it with git restore filename or git checkout -- filename. This brings back the last committed version of the file. If you also staged the deletion with git add, first run git restore --staged filename and then git restore filename.
Why Git Can Recover Your Deleted File
Git is a version control system, which means it keeps a snapshot of your committed files in its history. When you delete a file from your folder, you only delete the working copy at first.
The committed copy still lives safely inside Git until you record the deletion with a commit. That is why the timing matters so much.
Think of Git as having three main areas:
- Working directory: The real files and folders you see and edit on your computer.
- Staging area: The place where you prepare changes with
git addbefore committing. - Repository history: The committed snapshots that Git keeps permanently in your project history.
Deleting a file only affects the first area at first. The history still holds the file, so Git can copy it back for you in seconds.
If you are new to these ideas, our beginner-friendly GitHub and Git guide for new developers explains the working directory, staging, and commits with simple examples.
First, Check What Git Sees Right Now
Before you run any recovery command, check the status of your project. This tells you whether the deletion is staged or unstaged, and that decides which steps you need.
- Open a terminal inside your project folder.
- Run
git statusand read the output carefully. - If the file appears under
Changes not staged for commitwith the worddeleted, the deletion is unstaged. - If it appears under
Changes to be committed, the deletion is staged and you need one extra step. - Note the exact file name and path shown by Git, including folders, for example
src/login.js.
Do not run git add or git commit at this stage. You want to keep the deletion uncommitted while you recover the file.
How to Recover a Deleted File That Is Not Staged
This is the most common and easiest case. You deleted the file in your file explorer or editor, but you did not stage or commit anything after that.
Using git restore (Recommended)
- Run
git statusto confirm the file is listed as deleted but not staged. - Run
git restore filename, replacingfilenamewith your real file name and path, such asgit restore src/login.js. - Check your project folder. The file should be back with its last committed content.
- Run
git statusagain. The file should no longer appear as deleted.
The git restore command was added to make this job clearer for beginners. The official Git restore documentation describes all of its options in detail.
Using git checkout (Older Method)
On older Git versions, the same job was done with a different command. It still works today, and you will see it in many older tutorials.
- Run
git checkout -- filename, for examplegit checkout -- index.html. - The
--part tells Git that what follows is a file name, not a branch name. - Verify the file is back in your folder and check
git status.
Both commands give the same result. Use git restore if your version supports it.
How to Recover a File If You Already Staged the Deletion
Sometimes you run git add . after deleting a file, and that stages the deletion. The file is still recoverable, but you need two steps instead of one.
- Run
git statusand confirm the deletion is underChanges to be committed. - Run
git restore --staged filenameto unstage the deletion. The file is still deleted on disk at this point, which is normal. - Now run
git restore filenameto bring the actual file back from the last commit. - Run
git statusagain to confirm the file is clean and present.
You can also do it in one combined step on newer Git versions with git restore --source=HEAD --staged --worktree filename. For most beginners, the two simple steps above are easier to remember and less error-prone.
An older alternative is git reset HEAD filename followed by git checkout -- filename. It does the same job if you are following an older guide.
How to Recover Several Deleted Files at Once
If you deleted a whole folder or many files at once, restoring them one by one is slow. Git lets you restore everything in one go, but use this with care.
- Run
git statusand review every deleted and modified file in the list. - If you only want deletions back, restore the folder with
git restore foldername/, such asgit restore src/. - If you truly want to discard all unstaged changes, run
git restore .from the project root. - Check the folder and run
git statusto confirm the result.
Be very careful with git restore ., because it also discards edits you made to other files that you have not committed. If you changed three files and deleted one, that command will undo all four changes. When in doubt, restore files one by one.
What If You Deleted the File and Edited It Before?
There is one important limit you must understand. Git can only restore the last committed version of a file.
Imagine you edited a file for two hours, never committed, and then deleted it. Git never saw those edits, so it cannot bring them back. It will restore the older committed version instead.
This is not a flaw in Git. It is a reminder to commit small and often, so your recent work is always protected inside the history. Some editors also keep a local history or timeline feature, which is worth checking before you give up on uncommitted edits.
The official Git status documentation is helpful here too, because it explains exactly how Git reports deleted, modified, and staged files.
What If You Already Committed the Deletion?
If you already committed the deletion, this guide’s simple commands will not be enough, but the file is still not lost forever. The previous commit still contains it.
In that case, you can view history with git log --oneline, find the commit before the deletion, and restore the file from there using git restore --source=<commit-hash> filename. You can also revert the deleting commit with git revert <commit-hash>.
An uncommitted deletion is the easiest to fix, so act before you commit anything else.
Common Mistakes to Avoid
- Committing before you check: Once you commit a deletion, recovery takes extra steps. Always run
git statusbefore committing. - Using the wrong file path: If the file is inside a folder, you must include the full path, such as
git restore docs/guide.md, or Git will say the path does not match. - Restoring everything without checking:
git restore .discards all uncommitted edits, not only deletions. Review the status list first. - Confusing staged and unstaged deletions: A staged deletion needs
git restore --stagedfirst. Skipping that step confuses many beginners. - Waiting too long with uncommitted edits: Git protects committed work, not hours of unsaved changes. Commit early and often.
- Typing the file name from memory: Copy the exact name from the
git statusoutput. Capital letters and spelling must match.
Conclusion
Deleting a file before committing is one of the easiest Git accidents to fix. In most cases, a single git restore filename command brings back the last committed version in seconds.
Remember to check git status first, unstage the deletion if you need to, and be careful with commands that restore everything at once. Commit your work regularly, and Git will keep protecting you from these small mistakes.
Frequently Asked Questions
Will git restore bring back the exact version I deleted?
It brings back the last committed version of the file. If you made edits after your last commit and never committed them, those specific edits cannot be recovered by Git, because Git never recorded them.
What if I deleted a file with git rm instead of my file explorer?
git rm filename deletes the file and stages the deletion at the same time. Recover it by running git restore --staged filename first, followed by git restore filename.
Can I recover a file that was never committed at all?
No. If a file was created and deleted without ever being added and committed, Git has no copy of it. Check your operating system’s recycle bin or trash, and your editor’s local history, as your only remaining options.
What is the difference between git restore and git checkout for deleted files?
Both restore the committed version of the file. git restore is the newer, clearer command made for this job, while git checkout -- filename is the older method you will still see in many tutorials.
How do I recover a deleted folder in Git?
Run git restore foldername/ with the folder path, or restore each file inside it by name. If the deletion was staged, unstage the folder first with git restore --staged foldername/.


Leave a Reply