Recovering Deleted Committed Files
Occasionally, you may accidentally delete a file that has been committed to your Git repository. Fortunately, Git provides a way to recover these deleted files, as long as they have been committed at least once.
Using git restore
To recover a deleted committed file, you can use the git restore
command:
git restore path/to/deleted/file
This command will restore the file from the last commit and bring it back to your working directory.
Using git checkout
Alternatively, you can use the git checkout
command to recover a deleted committed file:
git checkout -- path/to/deleted/file
This command will also restore the file from the last commit and bring it back to your working directory.
Recovering from a Specific Commit
If you need to recover a file from a specific commit, you can use the commit hash along with the git restore
or git checkout
command:
git restore -s 1a2b3c4d path/to/deleted/file
or
git checkout 1a2b3c4d -- path/to/deleted/file
This will restore the file from the commit with the hash 1a2b3c4d
.
By understanding how to recover deleted committed files, you can more effectively manage your Git repository and avoid the loss of important code or data.