From 6ef998b024568b55077321c68625638b5920138f Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Sun, 2 Nov 2025 11:01:30 -0600 Subject: [PATCH] Add Check If A File Is Under Version Control as a Git TIL --- README.md | 3 +- ...heck-if-a-file-is-under-version-control.md | 36 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 git/check-if-a-file-is-under-version-control.md diff --git a/README.md b/README.md index 76d1d5f..dae93c1 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1677 TILs and counting..._ +_1678 TILs and counting..._ See some of the other learning resources I work on: @@ -322,6 +322,7 @@ If you've learned something here, support my efforts writing daily TILs by - [Change The Start Point Of A Branch](git/change-the-start-point-of-a-branch.md) - [Check How A File Is Being Ignored](git/check-how-a-file-is-being-ignored.md) - [Check If A File Has Changed In A Script](git/check-if-a-file-has-changed-in-a-script.md) +- [Check If A File Is Under Version Control](git/check-if-a-file-is-under-version-control.md) - [Checking Commit Ancestry](git/checking-commit-ancestry.md) - [Checkout Old Version Of A File](git/checkout-old-version-of-a-file.md) - [Checkout Previous Branch](git/checkout-previous-branch.md) diff --git a/git/check-if-a-file-is-under-version-control.md b/git/check-if-a-file-is-under-version-control.md new file mode 100644 index 0000000..e98bc60 --- /dev/null +++ b/git/check-if-a-file-is-under-version-control.md @@ -0,0 +1,36 @@ +# Check If A File Is Under Version Control + +The `git ls-files` command can be used with the `--error-unmatch` flag to check +if a file is under version control. It does this by checking if any of the +listed files appears on the _index_. If any does not, it is treated as an error. + +In a project, I have a `README.md` that is under version control. And I have +`node_modules` that shouldn't be under version control (which is why they are +listed in my `.gitignore` file). I can check the README and a file somewhere in +`node_modules`. + +```bash +❯ git ls-files --error-unmatch README.md +README.md + +❯ git ls-files --error-unmatch node_modules/@ai-sdk/anthropic/CHANGELOG.md +error: pathspec 'node_modules/@ai-sdk/anthropic/CHANGELOG.md' did not match any file(s) known to git +Did you forget to 'git add'? +``` + +Notice the second command results in an error because of the untracked +`CHANGELOG.md` file in `node_modules`. + +Here is another example of this at work while specifying multiple files: + +```bash +❯ git ls-files --error-unmatch README.md node_modules/@ai-sdk/anthropic/CHANGELOG.md package.json +README.md +package.json +error: pathspec 'node_modules/@ai-sdk/anthropic/CHANGELOG.md' did not match any file(s) known to git +Did you forget to 'git add'? +``` + +Each tracked file gets listed and then the untracked file results in an error. + +See `man git-ls-files` for more details.