This blog will discuss the method of getting a removed file in the Git project commit history.
How to Get a Deleted/Removed File in the Git Project Commit History?
Try the below-stated scenario to get the deleted file in the Git project to commit history:
- Switch to the Git local repository.
- Check the current working branch reference log history.
- Copy the commit id, which contains the deleted file details.
- Run the git show –pretty=“” –name-only <sha-hash>“ command.
- View the editing detail of the deleted file by running the “git show <sha-hash — <file-path>>” command.
Step 1: Move to Repository
First of all, developers need to navigate to the desired local repository with the help of the “cd” command:
Step 2: View Git Reference Log History
Then, execute the “git reflog .” command to show the entire history of the current working branch:
According to the below-given output, the highlighted commit contains the most recently deleted files details and copy its commit id:
Step 3: Find Project Deleted File
Now, execute the “git show” command to get the deleted project file:
Here, the:
- “–pretty= “”” option used for just showing the commit message briefly.
- “–name-only” will just get the deleted file name.
- “7f6…” is the commit id containing the changes’ details.
According to the below-given, the deleted file name is “file2.txt”, which is placed in the “perk1” Git local repository:
Step 4: View Deleted File Changes Detail
If you want to get the detail of the modification of the deleted file, then execute the “git show <commit-id> — <file-path>” command:
In the above-stated command, the “7f6….” is the commit SHA-hash, the “—” is used to tell Git, to consider the file which is provided after these special characters. As you can see, it displays the provided commit details along with the deleted file’s full modifications history:
That’s it! We have demonstrated finding a removed/deleted file in the Git project commit history.
Conclusion
To get the deleted file in the Git project to commit history, first, navigate to the Git local repository and check its reference log history. Then, copy the SHA hash of the commit, which contains the deleted file details. Next, execute the “git show –pretty=“” –name-only <sha-hash>” command. To view the editing detail of the deleted file, run the “git show <sha-hash — <file-path>>” command. This blog described the process of getting a removed/deleted file in the Git project commit history.