Git
How to Recover Deleted Files from GitHub?
Accidentally deleting files from a GitHub repository can be stressful, especially if those files contain critical code or documentation. Fortunately, Git and GitHub offer ways to recover deleted files by leveraging Git’s robust version control capabilities. In this blog, we’ll walk through several methods to restore deleted files, including using Git commands and GitHub’s web interface.
Why You Can Recover Deleted Files in Git
Git tracks every change made to your project, creating a history of commits that includes additions, modifications, and deletions. Each commit in Git represents a snapshot of your repository at a particular point in time. Because of this history, you can revert to previous versions, restore files from specific commits, and even undo accidental deletions.
Methods to Recover Deleted Files from GitHub
Here are some effective methods for recovering deleted files from GitHub:
- Using Git Command Line (Local Repository)
- Restoring Deleted Files from GitHub’s Web Interface
- Reverting to a Previous Commit
- Using
git reflog
for Recently Deleted Files
Let’s explore each method in detail.
Method 1: Using Git Command Line to Recover Deleted Files
If you’ve deleted a file and haven’t pushed the changes to GitHub yet, you can easily recover it from your local Git history.
Steps to Recover Deleted Files Locally
- Identify the Deleted Files Use
git status
to see which files have been deleted but not yet committed.
git status
- Recover Deleted Files Using
git restore
You can use thegit restore
command to bring back the deleted file:
git restore <file-name>
Replace <file-name>
with the path of the deleted file. This command restores the file from the latest commit, returning it to your working directory.
- Commit the Restored File Once the file is restored, commit the changes:
git add <file-name>
git commit -m "Restore deleted file <file-name>"
- Push to GitHub (If Needed) If the deletion was already pushed to GitHub, push the restored commit to update the remote repository:
git push origin <branch-name>
Method 2: Recover Deleted Files from GitHub’s Web Interface
If the file was deleted and pushed to GitHub, you can restore it directly from GitHub’s web interface.
Steps to Restore a File from GitHub’s Commit History
- Navigate to Your Repository on GitHub Go to the GitHub repository where the file was deleted.
- Access the Commit History Click on the Commits link on your repository’s main page to see a list of recent commits.
- Find the Commit Before the Deletion Identify the commit where the file was last present. This is typically the commit just before the one where the file was deleted.
- Browse the Files in That Commit Click on the specific commit to browse the files in that snapshot. You’ll see the repository as it was at that point in time.
- Locate the Deleted File Find the file that was deleted, click on it, then click View Raw or Copy content to download it.
- Add the File Back to Your Repository Once you have the deleted file, you can re-add it to your local repository and push the changes back to GitHub.
Method 3: Reverting to a Previous Commit
If multiple files were deleted or significant changes were made that you want to undo, you can revert your branch to an earlier commit. This approach is useful if the deletions were part of a recent commit that you want to undo.
Steps to Revert to a Previous Commit
- Find the Commit Hash Use
git log
to find the commit hash of the version you want to revert to. Each commit will have a unique hash identifier:
git log
- Use
git revert
to Undo the Commit To reverse a commit without altering history, use thegit revert
command:
git revert <commit-hash>
Replace <commit-hash>
with the hash of the commit where the file was deleted. This command creates a new commit that undoes the changes from the deleted file.
- Push the Reverted Commit to GitHub After reverting, push the changes to your GitHub repository:
git push origin <branch-name>
Note: Reverting a commit doesn’t remove it from the commit history; instead, it creates a new commit that effectively undoes the previous one.
Method 4: Using git reflog
to Restore Recently Deleted Files
If you deleted a file and committed the changes but didn’t push them, you can use git reflog
to track recent changes and recover the deleted file.
Steps to Use git reflog
for Recovery
- View Recent Actions Using
git reflog
git reflog
shows a history of your actions, including commits, resets, checkouts, and more.
git reflog
This will display a list of recent actions, with each entry showing a reference ID and a short description.
- Identify the Commit Before Deletion Find the commit where the file was still present. Note the reference ID of this commit.
- Reset to the Desired Commit Use
git reset
to return to the state of that commit:
git reset --hard <reference-id>
Replace <reference-id>
with the reference ID of the commit just before the file was deleted.
Warning:
git reset --hard
will discard all changes after the specified commit, so ensure you’ve backed up any recent work.
- Push the Changes to GitHub If necessary, push the restored version to GitHub:
git push --force origin <branch-name>
Tip: Be cautious when using
git reset --hard
, as it permanently deletes any uncommitted changes.
Best Practices for Avoiding Data Loss in Git
While Git provides powerful tools to recover deleted files, here are a few best practices to help avoid accidental deletions:
- Commit Frequently: Regular commits create a detailed history of changes, making it easier to revert unwanted changes.
- Use Branches for New Features: Working on separate branches isolates changes and reduces the risk of unintended modifications in the main branch.
- Use
git rm
Instead of Manual Deletion: Thegit rm
command stages deletions, which can then be reviewed before committing. - Double-Check Before Pushing: Review your staged changes with
git status
orgit diff
to ensure no files are accidentally deleted.
Summary
Accidental deletions can happen to anyone, but with Git and GitHub, you have multiple ways to restore deleted files. Here’s a quick recap of the methods covered:
- Using Git Command Line (Local Repository): Use
git restore
to recover recently deleted files before committing changes. - GitHub Web Interface: Retrieve deleted files from GitHub’s commit history and restore them manually.
- Revert to a Previous Commit: Reverse commits to restore files and undo multiple changes.
- Use
git reflog
: Recover recent deletions by referencing previous actions in Git.
By following these steps and best practices, you can minimize data loss and confidently manage your code in GitHub. With Git’s robust version control, restoring files is a straightforward process, keeping your projects safe and organized.