Git

How to Find a Deleted File in the Project Commit History?

On Git, developers can generate new files and add changes. They can also delete unused files to free up more space or clean the repository from spam data. Moreover, they can get the complete information of the deleted file with the help of the committed data. For this purpose, they need the specific commit SHA-hash which contains the deleted file data.

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:

$ cd "C:\Users\nazma\Git\perk1"

Step 2: View Git Reference Log History

Then, execute the “git reflog .” command to show the entire history of the current working branch:

$ git reflog .

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:

$ git show --pretty="" --name-only 7f690f8

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:

$ git show 7f690f8 -- perk1/

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.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.