如何判断文件是否被 git 跟踪(通过 shell 退出代码)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2405305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to tell if a file is git tracked (by shell exit code)?
提问by Dale Forester
Is there a way to tell if a file is being tracked by running some git
command and checking its exit code?
有没有办法通过运行一些git
命令并检查其退出代码来判断文件是否正在被跟踪?
In other words: is git tracking a file?
换句话说:git 是否在跟踪文件?
回答by hasen
try:
尝试:
git ls-files --error-unmatch <file name>
will exit with 1 if file is not tracked
如果未跟踪文件,则以 1 退出
回答by cheesus
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.
然后检查结果。如果 git 不返回任何内容,则不会跟踪该文件。如果它被跟踪,git 将返回文件路径。
This comes in handy if you want to combine it in a script, for example PowerShell:
如果你想将它组合到一个脚本中,这会派上用场,例如 PowerShell:
$gitResult = (git ls-files $_) | out-string
if ($gitResult.length -ne 0)
{
## do stuff with the tracked file
}
回答by stefanB
EDIT
编辑
If you need to use git from bash there is --porcelain
option to git status
:
如果您需要从 bash 使用 git,则可以--porcelain
选择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.
- 瓷
以稳定、易于解析的格式为脚本提供输出。目前这与 --short 输出相同,但保证将来不会更改,使其对脚本安全。
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:
可以看到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")
回答by JaredPar
Try running git status
on the file. It will print an error if it's not tracked by git
尝试git status
在文件上运行。如果 git 没有跟踪它,它将打印一个错误
PS$> git status foo.txt
error: pathspec 'foo.txt' did not match any file(s) known to git.
回答by Jay Walker
I don't know of any git command that gives a "bad" exit code, but it seems like an easy way to do it would be to use a git command that gives no output for a file that isn't tracked, such as git-log or git-ls-files. That way you don't really have to do any parsing, you can run it through another simple utility like grep to see if there was any output.
我不知道任何给出“错误”退出代码的 git 命令,但似乎一种简单的方法是使用 git 命令,该命令不为未跟踪的文件提供输出,例如git-log 或 git-ls-files。这样您就不必真正进行任何解析,您可以通过另一个简单的实用程序(如 grep)运行它以查看是否有任何输出。
For example,
例如,
git-ls-files test_file.c | grep .
git-ls-files test_file.c | 格雷普。
will exit with a zero code if the file is tracked, but a exit code of one if the file is not tracked.
如果文件被跟踪,将以零代码退出,但如果文件未被跟踪,则退出代码为 1。
回答by Luca Davanzo
I suggest a custom aliason you .gitconfig
.
我建议你自定义别名.gitconfig
。
You have to way to do:
你必须这样做:
1) With git command:
1) 使用 git 命令:
git config --global alias.check-file <command>
2) Editing ~/.gitconfig
and add this line on alias section:
2)~/.gitconfig
在别名部分编辑并添加这一行:
[alias]
check-file = "!f() { if [ $# -eq 0 ]; then echo 'Filename missing!'; else tracked=$(git ls-files ); 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:
启动命令 (1) 或保存文件 (2) 后,您可以在您的工作区中对其进行测试:
$ git check-file
$ Filename missing
$ git check-file README.md
$ File tracked
$ git check-file foo
$ File not tracked
回答by SuperNova
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.
使用git log
将提供有关此的信息。如果在 git 中跟踪文件,则该命令会显示一些结果(日志)。否则为空。
For example if the file is git tracked,
例如,如果文件是 git 跟踪的,
root@user-ubuntu:~/project-repo-directory# git log src/../somefile.js
commit ad9180b772d5c64dcd79a6cbb9487bd2ef08cbfc
Author: User <[email protected]>
Date: Mon Feb 20 07:45:04 2017 -0600
fix eslint indentation errors
....
....
If the file is not git tracked,
如果文件没有被 git 跟踪,
root@user-ubuntu:~/project-repo-directory# git log src/../somefile.js
root@user-ubuntu:~/project-repo-directory#
回答by andreee
Just my two cents:
只是我的两分钱:
git ls-files | grep -x relative/path
git ls-files | grep -x relative/path
where relative/path
can be easily determined by pressing tab
within an auto-completion shell. Add an additional | wc -l
to get a 1 or 0 output.
relative/path
可以通过tab
在自动完成外壳内按下来轻松确定在哪里。添加一个额外的| wc -l
以获得 1 或 0 的输出。