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

后端 未结 8 2170
刺人心
刺人心 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:05

    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
    }
    

提交回复
热议问题