Restore a deleted GitHub file quickly with our step-by-step guide: locate the commit, checkout the file, stage, commit, and push your changes back to GitHub.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Step 1: Identify the commit where the file was deleted
First, you need to find the SHA-1 hash (commit ID) of the commit just before the file deletion. Limiting the log to the file path shows you only relevant entries.
path/to/your/file
with the deleted file’s path:
git log -- path/to/your/file
Step 2: Check out the file from that commit
Once you have the commit hash (for example abc1234
), you can retrieve the deleted file into your working directory without altering your current branch.
git checkout
specifying the commit hash and file path:
git checkout abc1234 -- path/to/your/file
This restores the file state from commit abc1234
into your working directory.
Step 3: Stage the restored file
With the file back in your working directory, you need to add it to the staging area so it can be committed.
git add path/to/your/file
Step 4: Commit the restoration
Create a new commit that records the file restoration on your current branch.
git commit -m "Restore deleted file path/to/your/file"
Step 5: Push your changes to GitHub
Finally, upload your new commit to the remote repository so the file is restored on GitHub.
main
(or another branch), push with:
git push origin main
After the push completes, navigate to your GitHub repository to confirm that the deleted file has been restored.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.