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

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

    try:

    git ls-files --error-unmatch <file name>
    

    will exit with 1 if file is not tracked

    0 讨论(0)
  • 2020-11-28 18:23

    using git log will give info about this. If the file is tracked in git the command shows some results(logs). Else it is empty.

    For example if the file is git tracked,

    root@user-ubuntu:~/project-repo-directory# git log src/../somefile.js
    commit ad9180b772d5c64dcd79a6cbb9487bd2ef08cbfc
    Author: User <someone@somedomain.com>
    Date:   Mon Feb 20 07:45:04 2017 -0600
    
        fix eslint indentation errors
    ....
    ....
    

    If the file is not git tracked,

    root@user-ubuntu:~/project-repo-directory# git log src/../somefile.js
    root@user-ubuntu:~/project-repo-directory#
    
    0 讨论(0)
提交回复
热议问题