git 如何检查当前分支中是否没有要提交的内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5139290/
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 check if there's nothing to be committed in the current branch?
提问by 9nix00
The goal is to get an unambiguous status that can be evaluated in a shell command.
目标是获得可以在 shell 命令中评估的明确状态。
I tried git status
but it always returns 0, even if there are items to commit.
我试过了,git status
但它总是返回 0,即使有项目要提交。
git status
echo $? #this is always 0
I have an idea but I think it is rather a bad idea.
我有一个主意,但我认为这是一个相当糟糕的主意。
if [ git status | grep -i -c "[a-z]"> 2 ];
then
code for change...
else
code for nothing change...
fi
any other way?
任何其他方式?
update with following solve, see Mark Longair's post
使用以下解决方案进行更新,请参阅 Mark Longair 的帖子
I tried this but it causes a problem.
我试过这个,但它会导致一个问题。
if [ -z $(git status --porcelain) ];
then
echo "IT IS CLEAN"
else
echo "PLEASE COMMIT YOUR CHANGE FIRST!!!"
echo git status
fi
I get the following error [: ??: binary operator expected
我收到以下错误 [: ??: binary operator expected
now, I am looking at the man and try the git diff.
现在,我正在看着这个人并尝试使用 git diff。
===================code for my hope, and hope better answer======================
==================我希望的代码,希望更好的答案======================
#if [ `git status | grep -i -c "$"` -lt 3 ];
# change to below code,although the above code is simple, but I think it is not strict logical
if [ `git diff --cached --exit-code HEAD^ > /dev/null && (git ls-files --other --exclude-standard --directory | grep -c -v '/$')` ];
then
echo "PLEASE COMMIT YOUR CHANGE FIRST!!!"
exit 1
else
exit 0
fi
回答by Mark Longair
An alternative to testing whether the output of git status --porcelain
is empty is to test each condition you care about separately. One might not always care, for example, if there are untracked files in the output of git status
.
测试输出是否git status --porcelain
为空的另一种方法是分别测试您关心的每个条件。例如,人们可能并不总是关心git status
.
For example, to see if there are any local unstaged changes, you can look at the return code of:
例如,要查看是否有任何本地未暂存的更改,可以查看以下返回码:
git diff --exit-code
To check if there are any changes that are staged but not committed, you can use the return code of:
要检查是否有任何已暂存但未提交的更改,您可以使用以下返回代码:
git diff --cached --exit-code
Finally, if you want to know about whether there are any untracked files in your working tree that aren't ignored, you can test whether the output of the following command is empty:
最后,如果您想知道您的工作树中是否有任何未被忽略的未跟踪文件,您可以测试以下命令的输出是否为空:
git ls-files --other --exclude-standard --directory
Update:You ask below whether you can change that command to exclude the directories in the output. You can exclude empty directories by adding --no-empty-directory
, but to exclude all directories in that output I think you'll have to filter the output, such as with:
更新:您在下面询问是否可以更改该命令以排除输出中的目录。您可以通过添加排除空目录--no-empty-directory
,但要排除该输出中的所有目录,我认为您必须过滤输出,例如:
git ls-files --other --exclude-standard --directory | egrep -v '/$'
The -v
to egrep
means to only output lines that don't match the pattern, and the pattern matches any line that ends with a /
.
在-v
到egrep
装置仅输出线不匹配的图案,并且图案的任何线与一个端部相匹配/
。
回答by eckes
The return value of git status
just tells you the exit code of git status
, not if there are any modifications to be committed.
的返回值git status
只是告诉您 的退出代码git status
,而不是是否有任何修改要提交。
If you want a more computer-readable version of the git status
output, try
如果您想要更计算机可读的git status
输出版本,请尝试
git status --porcelain
See the description of git status
for more information about that.
有关git status
更多信息,请参阅 的说明。
Sample use (script simply tests if git status --porcelain
gives any output, no parsing needed):
示例使用(脚本仅测试是否git status --porcelain
提供任何输出,无需解析):
if [ -n "$(git status --porcelain)" ]; then
echo "there are changes";
else
echo "no changes";
fi
Please note that you have to quote the string to test, i.e. the output of git status --porcelain
. For more hints about test constructs, refer to the Advanced Bash Scripting Guide(Section string comparison).
请注意,您必须引用要测试的字符串,即git status --porcelain
. 有关测试构造的更多提示,请参阅高级 Bash 脚本指南(字符串比较部分)。
回答by moodboom
If you are like me, you want to know if there are:
如果你和我一样,你想知道是否有:
1) changes to existing files 2) newly added files 3) deleted files
1) 对现有文件的更改 2) 新添加的文件 3) 删除的文件
and specifically do not want to know about 4) untracked files.
并且特别不想知道 4) 未跟踪的文件。
This should do it:
这应该这样做:
git status --untracked-files=no --porcelain
Here's my bash code to exit the script if the repo is clean. It uses the short version of the untracked files option:
如果 repo 是干净的,这是我的 bash 代码退出脚本。它使用未跟踪文件选项的简短版本:
[[ -z $(git status -uno --porcelain) ]] && echo "this branch is clean, no need to push..." && kill -SIGINT $$;
回答by Steve Prentice
It's possible to combine git status --porcelain
with a simple grep
to perform the test.
可以结合git status --porcelain
一个简单的grep
来执行测试。
if git status --porcelain | grep .; then
echo Repo is dirty
else
echo Repo is clean
fi
I use this as a simple one-liner sometimes:
我有时将其用作简单的单行:
# pull from origin if our repo is clean
git status --porcelain | grep . || git pull origin master
Add -qs
to your grep command to make it silent.
添加-qs
到您的 grep 命令以使其静音。
回答by archf
i'd do a test on this:
我会对此进行测试:
git diff --quiet --cached
or this to be explicit:
或者这是明确的:
git diff --quiet --exit-code --cached
where:
在哪里:
--exit-code
--退出代码
Make the program exit with codes similar to diff(1). That is, it exits with 1 if there were differences and 0 means no differences.
使用类似于 diff(1) 的代码使程序退出。也就是说,如果存在差异,则以 1 退出,0 表示没有差异。
--quiet
- 安静的
Disable all output of the program. Implies --exit-code
禁用程序的所有输出。暗示 --exit-code
回答by Arrowmaster
From the git source code there is a sh script which includes the following.
从 git 源代码中有一个 sh 脚本,其中包括以下内容。
require_clean_work_tree () {
git rev-parse --verify HEAD >/dev/null || exit 1
git update-index -q --ignore-submodules --refresh
err=0
if ! git diff-files --quiet --ignore-submodules
then
echo >&2 "Cannot : You have unstaged changes."
err=1
fi
if ! git diff-index --cached --quiet --ignore-submodules HEAD --
then
if [ $err = 0 ]
then
echo >&2 "Cannot : Your index contains uncommitted changes."
else
echo >&2 "Additionally, your index contains uncommitted changes."
fi
err=1
fi
if [ $err = 1 ]
then
test -n "" && echo >&2 ""
exit 1
fi
}
This sniplet shows how its possible to use git diff-files
and git diff-index
to find out if there are any changes to previously known files. It does not however allow you to find out if a new unknown file has been added to the working tree.
此 sniplet 展示了如何使用它git diff-files
并git diff-index
找出对先前已知文件是否有任何更改。但是,它不允许您查明是否已将新的未知文件添加到工作树中。
回答by Skeeve
I'm a bit late in the discussion, but if it's only that you need to have an exit code of 0 if git status --porcelain
returns nothing and != 0 else, try this:
我在讨论中有点晚了,但是如果您只需要退出代码为 0 如果不git status --porcelain
返回任何内容并且 != 0 其他,请尝试以下操作:
exit $( git status --porcelain | wc -l )
This will make the number of lines be the exit code, at the risk of getting issues when there's more than 255 lines. So
这将使行数成为退出代码,当超过 255 行时有出现问题的风险。所以
exit $( git status --porcelain | head -255 | wc -l )
will account for that ;)
将说明这一点;)
回答by grosser
I'm using this in a script to have:
我在脚本中使用它来拥有:
- 0 when everything is clean
1 when there is a diff or untracked files
[ -z "$(git status --porcelain)" ]
- 0 当一切都干净时
1 当存在差异或未跟踪的文件时
[ -z "$(git status --porcelain)" ]
回答by Tilman Vogel
Not pretty, but works:
不漂亮,但有效:
git status | grep -qF 'working directory clean' || echo "DIRTY"
Not sure whether the message is locale dependent, so maybe put a LANG=C
in front.
不确定消息是否依赖LANG=C
于语言环境,所以可能在前面放一个。