Find git commits that delete files
While working on a Django ticket, I found myself
doing some git archeology and learned a new (to) me flag for git log which I thought I'd share.
The problem statement
I wanted to find commits that had removed or renamed a file inside a specific directory (docs/ in
this case).
I've been doing git archeology for long enough that I know that when the problem statement starts with "I want
to find a commit that ..." then the solution probably involves git log and this time was no
different.
None of my searches were turning up anything interesting, only solutions for "how to find the commit where
file X was deleted", so I decided to read through the manual for git log.
After some trial and error involving --stat and --dirstat, I finally found what I
wanted by looking for the word "deleted" inside the man page.
The solution: --diff-filter
Here's the command I ended up with:
git log --diff-filter=RD --stat -- docs/
Some explanations:
-
git logwill list commits (all of them by default, in reverse order from newest to oldest); -
--diff-filter=RDwill only list commits that either deleted (D) or renamed (R) a file; -
--statwill show some statistics on the commit, in that case showing which was was deleted/renamed; -
finally,
-- docs/restricts the commits to those involving files inside thedocs/directory.
Have fun exploring your git repositories! ⛏️