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

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

    I suggest a custom alias on you .gitconfig.

    You have to way to do:

    1) With git command:

    git config --global alias.check-file 
    

    2) Editing ~/.gitconfig and add this line on alias section:

    [alias]
        check-file = "!f() { if [ $# -eq 0 ]; then echo 'Filename missing!'; else tracked=$(git ls-files ${1}); if [[ -z ${tracked} ]]; then echo 'File not tracked'; else echo 'File tracked'; fi; fi;  };  f"
    

    Once launched command (1) or saved file (2), on your workspace you can test it:

    $ git check-file
    $ Filename missing 
    
    $ git check-file README.md
    $ File tracked 
    
    $ git check-file foo
    $ File not tracked
    

提交回复
热议问题