Is there a way to tell if a file is being tracked by running some git command and checking its exit code?
git
In other words: is git tracking a file?
Try running git status on the file. It will print an error if it's not tracked by git
git status
PS$> git status foo.txt error: pathspec 'foo.txt' did not match any file(s) known to git.