Recovering Deleted Files in GitHub:
To restore a deleted file using the Git command line you can use the git restore
or git checkout
command. Whenever you modify files in Git—including creating new files, editing, or deleting existing files—the changes start as unstaged. Then you stage the changes with the git add
command, and finally, you commit the changes using the git commit
command. Git provides ways to recover a deleted file at any point in this life cycle of changes.
This article you learn that how you can restore deleted file ? You can find and restored deleted file even you have deleted it months ago.
There are several ways to find and restore a deleted file in a Git repository. Here are a few:
1. Use the git log
command to find the commit where the file was deleted. The git log
command will show a list of all commits in the repository, and you can use the -S
option to search for commits that contain a specific string (the file name). Once you find the commit where the file was deleted, you can use the commit hash to restore the file.
git log -S <file_name>
2. Use the git reflog
command to find the commit where the file was deleted. The git reflog
command shows a list of all the changes made to the repository, including commits, branch switches, and other operations. Once you find the commit where the file was deleted, you can use the commit hash to restore the file.
git reflog
3. Use the git checkout command to restore the file. Once you have the commit hash where the file was deleted, you can use the git checkout command to restore the file to the state it was in at that commit.
git checkout <commit_hash> -- <file_name>
4. Use the git revert
command to undo the commit that deleted the file. This will create a new commit that undoes the changes made in the commit where the file was deleted.
git revert <commit_hash>
5. Use the git restore
command to restore the file. This command is similar to git checkout
, but it can be used to restore both deleted and modified files.
git restore <file_name>
Please note that, if you want to restore the deleted file to a specific branch, you need to use the branch name instead of the commit hash in the above commands.
Please note that, after restore the deleted file, you need to commit the changes, otherwise the file will remain in the working tree but the changes are not tracked by git.