How to tell if a file is git tracked (by shell exit code)?

后端 未结 8 2162
刺人心
刺人心 2020-11-28 17:24

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?

8条回答
  •  情书的邮戳
    2020-11-28 18:15

    I don't know of any git command that gives a "bad" exit code, but it seems like an easy way to do it would be to use a git command that gives no output for a file that isn't tracked, such as git-log or git-ls-files. That way you don't really have to do any parsing, you can run it through another simple utility like grep to see if there was any output.

    For example,

    git-ls-files test_file.c | grep .

    will exit with a zero code if the file is tracked, but a exit code of one if the file is not tracked.

提交回复
热议问题