Is there a way to tell if a file is being tracked by running some git
command and checking its exit code?
In other words: is git tracking a file?
using git log will give info about this. If the file is tracked in git the command shows some results(logs).
Tracked files are files that were in the last snapshot, as well as any newly staged files; they can be unmodified, modified, or staged. In short, tracked files are files that Git knows about.
ls-tree will output all the versioned files.
try:
git ls-files --error-unmatch <file name>
will exit with 1 if file is not tracked
If you don't want to clutter up your console with error messages, you can also run
git ls-files file_name
and then check the result. If git returns nothing, then the file is not tracked. If it's tracked, git will return the file path.
This comes in handy if you want to combine it in a script, for example PowerShell:
$gitResult = (git ls-files $_) | out-string if ($gitResult.length -ne 0) { ## do stuff with the tracked file }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With