This is a 2 min read ยท Published on 07 Dec 2021

Today at work I noticed a file had been deleted in our repository. I wasn't sure who deleted it or why it was deleted. I really wanted to figure out which GitHub PR removed the file so I could get some context!

An example

To test this out, without revealing any of my work code, I created a new file named foo in this repo. Then I made a PR deleting that file and merged it into main.

Step 1: Find the commit that deleted the file

This took a bit of searching, but I was able to find on Stack Overflow a git command that will show you the full history of a file, even if it has been deleted.

git log --full-history  -- [file path]

So, for my foo file, I typed in:

git log --full-history  -- foo

And got back:

commit 63ca48a2cc63196df5428ddb0c09cda3002a8da3 (HEAD -> master, origin/master, origin/HEAD)
Merge: b7f2ad2 c48771f
Author: Jon Kuperman <[email protected]>
Date:   Tue Dec 7 13:38:00 2021 -0500

    Merge pull request #14 from jkup/sample-pr

    Delete foo (you found me)

commit c48771fd5dadf28d456be9202079dd95a0cff47a (origin/sample-pr, sample-pr)
Author: Jon Kuperman <[email protected]>
Date:   Tue Dec 7 13:37:39 2021 -0500

    Delete foo (you found me)

commit b7f2ad23d2d19fb7d589f993a87946141fb9cbae
Author: Jon Kuperman <[email protected]>
Date:   Tue Dec 7 13:37:22 2021 -0500

    add foo

Step 2: Grab the git SHA

Now I can see the entire history of the file. I want to see when it was deleted and that means I want to see the very last commit that touched it. Therefore, I'll copy the SHA at the top of the output, 63ca48a2cc63196df5428ddb0c09cda3002a8da3.

Step 3: A cool GitHub URL

The final step is figuring out what PR contained the offending commit. For that we can use this neat GitHub URL:

For our example we have:

  • username: jkup
  • projects:
  • sha: 63ca48a2cc63196df5428ddb0c09cda3002a8da3

So our final URL is:

Step 4: Click the PR

The last bit is just clicking through the GitHub UI to go from commit to PR. If you look at the top of the link above, you'll see the PR in parenthesis. In this case it's #14. Click on that and it will take you to the offending PR!

GitHub find PR for deleted file

And there it is, the PR that deleted foo from my repo:

Subscribe to my email list!

Let me be real with you. Sometimes when I'm bored I log in to Buttondown and look at the audience number. If it's bigger than it was the week before, well that makes me feel really happy! I promise I'll never spam you and I will, at most, send you a monthly update with what's happening on this site.