1 min read

Identifying when a file was deleted in Git

Sometimes I need to find when a file was deleted from Git; to see why it was removed, or I may need to recover the file from the latest point.
Identifying when a file was deleted in Git
Photo by Sam Pak / Unsplash

I needed to identify when a file was deleted, to find out why it was removed in the commit message. A quick search led me to this Stack Overflow page, which eventually ended here.

git log --all -- [filepath]

This will list out the commits that affect this file specifically, including the last one which removed it.

Here is a fun little adjustment to it which makes it format just a little bit easier at a glance.

Reference

  • %h - abbreviated commit hash
  • %d - ref names, like the --decorate option of git-log[1]
  • %s - subject
  • %ar - author date, relative (instead of %cr, which is commit date, since rebase can change the commit date)
  • %an - author name
git log --all --pretty=format:'%h -%d %s (%ar) <%an>' -- bitbucket-pipelines.yml
3e6caf229 - Remove puppet deploy from pipeline (2 years, 1 month ago) <Jack Stupple>
b32889c75 - bitbucket-pipelines.yml edited online with Bitbucket (2 years, 2 months ago) <Jack Stupple>