Skip to content

Commit 6ef998b

Browse files
committed
Add Check If A File Is Under Version Control as a Git TIL
1 parent 6e066ec commit 6ef998b

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010

1111
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
1212

13-
_1677 TILs and counting..._
13+
_1678 TILs and counting..._
1414

1515
See some of the other learning resources I work on:
1616

@@ -322,6 +322,7 @@ If you've learned something here, support my efforts writing daily TILs by
322322
- [Change The Start Point Of A Branch](git/change-the-start-point-of-a-branch.md)
323323
- [Check How A File Is Being Ignored](git/check-how-a-file-is-being-ignored.md)
324324
- [Check If A File Has Changed In A Script](git/check-if-a-file-has-changed-in-a-script.md)
325+
- [Check If A File Is Under Version Control](git/check-if-a-file-is-under-version-control.md)
325326
- [Checking Commit Ancestry](git/checking-commit-ancestry.md)
326327
- [Checkout Old Version Of A File](git/checkout-old-version-of-a-file.md)
327328
- [Checkout Previous Branch](git/checkout-previous-branch.md)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Check If A File Is Under Version Control
2+
3+
The `git ls-files` command can be used with the `--error-unmatch` flag to check
4+
if a file is under version control. It does this by checking if any of the
5+
listed files appears on the _index_. If any does not, it is treated as an error.
6+
7+
In a project, I have a `README.md` that is under version control. And I have
8+
`node_modules` that shouldn't be under version control (which is why they are
9+
listed in my `.gitignore` file). I can check the README and a file somewhere in
10+
`node_modules`.
11+
12+
```bash
13+
❯ git ls-files --error-unmatch README.md
14+
README.md
15+
16+
❯ git ls-files --error-unmatch node_modules/@ai-sdk/anthropic/CHANGELOG.md
17+
error: pathspec 'node_modules/@ai-sdk/anthropic/CHANGELOG.md' did not match any file(s) known to git
18+
Did you forget to 'git add'?
19+
```
20+
21+
Notice the second command results in an error because of the untracked
22+
`CHANGELOG.md` file in `node_modules`.
23+
24+
Here is another example of this at work while specifying multiple files:
25+
26+
```bash
27+
❯ git ls-files --error-unmatch README.md node_modules/@ai-sdk/anthropic/CHANGELOG.md package.json
28+
README.md
29+
package.json
30+
error: pathspec 'node_modules/@ai-sdk/anthropic/CHANGELOG.md' did not match any file(s) known to git
31+
Did you forget to 'git add'?
32+
```
33+
34+
Each tracked file gets listed and then the untracked file results in an error.
35+
36+
See `man git-ls-files` for more details.

0 commit comments

Comments
 (0)