From a1ccfb0ac1e8a48ea246879d7172f53abb4c771d Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Mon, 13 Jul 2026 17:43:39 -0500 Subject: [PATCH] Add List All Commits Where File Was Added Or Deleted as a Git TIL --- README.md | 3 +- ...commits-where-file-was-added-or-deleted.md | 36 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 git/list-all-commits-where-file-was-added-or-deleted.md diff --git a/README.md b/README.md index 0a1c7a7..6854b00 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/). For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter). -_1819 TILs and counting..._ +_1820 TILs and counting..._ See some of the other learning resources I work on: @@ -396,6 +396,7 @@ If you've learned something here, support my efforts writing daily TILs by - [Keep File Locally With `git rm`](git/keep-file-locally-with-git-rm.md) - [Last Commit A File Appeared In](git/last-commit-a-file-appeared-in.md) - [List All Authors On Git Repository](git/list-all-authors-on-git-repository.md) +- [List All Commits Where File Was Added Or Deleted](git/list-all-commits-where-file-was-added-or-deleted.md) - [List All Files Added During Span Of Time](git/list-all-files-added-during-span-of-time.md) - [List All Files Changed Between Two Branches](git/list-all-files-changed-between-two-branches.md) - [List All Git Aliases From gitconfig](git/list-all-git-aliases-from-gitconfig.md) diff --git a/git/list-all-commits-where-file-was-added-or-deleted.md b/git/list-all-commits-where-file-was-added-or-deleted.md new file mode 100644 index 0000000..193a999 --- /dev/null +++ b/git/list-all-commits-where-file-was-added-or-deleted.md @@ -0,0 +1,36 @@ +# List All Commits Where File Was Added Or Deleted + +I noticed I wasn't able to find a file with a specific name anywhere in my +codebase. I expected it to be there, so I wondered when it had been added and +deleted from the codebase according to the git commit history. + +This calls for running a `git log`, but with a couple flags. First, I'll include +`--name-status` so that each commit that is listed includes the file names with +the change status (i.e. `A` for added and `D` for deleted). Then the +`--diff-filter` flag tells git that I am only looking for `A`dded and `D`eleted +files. Then the `--` indicates that a file path will follow, even a regex +pattern is valid here. + +```bash +git log --name-status --diff-filter=AD -- '*_mock_payment_form.*' +``` + +I'm looking for what is called a _partial_ in Rails. I know the filename +consists of `_mock_payment_form`. The leading `*` is so that I can be vague +about the directory this might be found in. That's useful if this file was +potentially moved around. The trailing `.*` indicates the file extension which I +also want to be vague about. + +For any of the commits returned by this `git log`, I can grab the SHA and run +`git show ` to see the full picture of that commit. + +Another flag that might be useful to add is the `--all` flag which will look +across all refs/branches instead of just the branch (`main`) that I'm currently +on. + +```bash +git log --all --name-status --diff-filter=AD -- '*_mock_payment_form.*' +``` + +Perhaps I'm thinking of a file that was added on an abandoned feature branch. +`--all` will help turn up that in the results as well.