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

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

    EDIT

    If you need to use git from bash there is --porcelain option to git status:

    --porcelain

    Give the output in a stable, easy-to-parse format for scripts. Currently this is identical to --short output, but is guaranteed not to change in the future, making it safe for scripts.

    Output looks like this:

    > git status --porcelain
     M starthudson.sh
    ?? bla
    

    Or if you do only one file at a time:

    > git status --porcelain bla
    ?? bla
    

    ORIGINAL

    do:

    git status
    

    You will see report stating which files were updated and which ones are untracked.

    You can see bla.sh is tracked and modified and newbla is not tracked:

    # On branch master
    # Changed but not updated:
    #   (use "git add ..." to update what will be committed)
    #
    #       modified:   bla.sh
    #
    # Untracked files:
    #   (use "git add ..." to include in what will be committed)
    #
    #       newbla
    no changes added to commit (use "git add" and/or "git commit -a")
    

提交回复
热议问题