有没有一种方法可以通过运行一些git命令并检查其退出码来判断一个文件是否正在被跟踪?
换句话说:git在跟踪文件吗?
有没有一种方法可以通过运行一些git命令并检查其退出码来判断一个文件是否正在被跟踪?
换句话说:git在跟踪文件吗?
当前回答
使用git日志将提供有关此的信息。如果文件在git中被跟踪,该命令会显示一些结果(日志)。否则就是空的。
例如,如果文件被git跟踪,
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
....
....
如果文件没有git跟踪,
root@user-ubuntu:~/project-repo-directory# git log src/../somefile.js
root@user-ubuntu:~/project-repo-directory#
其他回答
EDIT
如果你需要从bash使用git,有——瓷器选项git状态:
——瓷 以稳定、易于解析的格式给出输出 脚本。目前这是相同的 -短输出,但保证 不改变的未来,造就的未来 脚本安全。
输出如下所示:
> git status --porcelain
M starthudson.sh
?? bla
或者如果你一次只做一个文件:
> git status --porcelain bla
?? bla
原始
do:
git status
您将看到报告,说明哪些文件已更新,哪些文件未跟踪。
你可以看到bla.sh被跟踪和修改,而newbla没有被跟踪:
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
#
# modified: bla.sh
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# newbla
no changes added to commit (use "git add" and/or "git commit -a")
我的观点是:
Git ls-files | grep -x相对/路径
在自动补全shell中,可以通过按TAB键轻松确定相对/路径。添加额外的| wc -l以获得1或0输出。
使用git日志将提供有关此的信息。如果文件在git中被跟踪,该命令会显示一些结果(日志)。否则就是空的。
例如,如果文件被git跟踪,
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
....
....
如果文件没有git跟踪,
root@user-ubuntu:~/project-repo-directory# git log src/../somefile.js
root@user-ubuntu:~/project-repo-directory#
尝试在文件上运行git status。如果git没有跟踪它,它将打印一个错误
PS$> git status foo.txt
error: pathspec 'foo.txt' did not match any file(s) known to git.
我不知道有哪个git命令给出了一个“糟糕的”退出码,但似乎有一种简单的方法可以做到这一点,那就是使用一个git命令,它不会为没有跟踪的文件提供输出,比如git-log或git-ls-files。这样就不需要进行任何解析,可以通过另一个简单的实用程序(如grep)来运行它,以查看是否有任何输出。
例如,
Git-ls-files test_file.c | grep。
如果文件被跟踪,将以0代码退出,但如果文件未被跟踪,则退出代码为1。