As a part of GIT Tutorials End To End, we will learn about retrieving a deleted and committed file in Git by using GIT reset command.
I will expect that you are aware of the basic concepts and commands of GIT. But if you are a beginner in GIT then I strongly recommend you to refer GIT Basic Commands and Concepts section on my blog first.
You must go through the below posts to understand the flow –
Retrieving A Deleted and committed File in Git
In the previous post, we deleted the file but did not commit it. In this post, the scenario will be a little different. I will delete the file and will commit it as well. Please note that the commit will be local and will not be pushed to the remote repo.
Let’s create a GIT repository and some files with contents.
Add and commit.
Now let’s delete a file and commit the change i.e. deletion.
In the previous post, we recovered the deleted file using the checkout command because the deleted file was not committed. Let’s run the same command again in this scenario.
We will see an error as above. As I have explained in my previous post that deleted file is recovered from the last commit i.e. HEAD. We have deleted the file but the deletion was not committed. So HEAD was still pointing to the commit in which deleted file was present. In this post, we have deleted the file and also committed the changes. Committing the changes will create a new commit id and HEAD will point to that. So when we used the checkout command in this condition, GIT tries to recover the file from the latest commit i.e. HEAD but the latest commit does not have that file because the latest commit actually has changes to delete the file.
In the last post, we already learned to retrieve a deleted file from a previous commit. In this post, we will use the git reset command to retrieve the deleted file.
I have already explained the Git reset command with soft, mixed and hard options. I will just undo the last commit with a mixed option.
We are at the stage when we actually deleted the file and now we have a chance to restore it.
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe to my YouTube channel.