如何检查当前分支中是否没有提交? [英] How to check if there's nothing to be committed in the current branch?

查看:178
本文介绍了如何检查当前分支中是否没有提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标是获得可以在shell命令中评估的明确状态。



我试过 git status ,但它总是返回0,即使有要提交的项目。

  git status 
echo $? #这是总是0

我有一个想法,但我认为这是一个坏主意。 p>

  if [git status | grep -i -c[a-z]> 2]; 
然后
代码改变...
else
代码没有任何变化...
fi

任何其他方式?






现在这个解决方案看到Mark Longair的帖子



我试过这个,但是造成了问题。

  if [-z $(git status --porcelain)]; 
然后
回声它是干净的
其他
回声请提前改变您的改变!!!
echo git status
fi

当$ / b
$ b

它会得到一些错误,[:??:二元运算符预计



现在,我正在查看man并尝试git diff。



===================代码为我的希望,希望更好的回答======= ===============

  #if [`git status | grep -i -c$`-lt 3]; 
#更改为下面的代码,虽然上面的代码很简单,但我认为这不是严格的逻辑
如果[`git diff --cached --exit-code HEAD ^> / dev / null&& (git ls-files --other --exclude-standard --directory | grep -c -v'/ $')`];
然后
回显请提交您的更改!
出口1

其他
出口0
fi


git status --porcelain 的输出是否为空的替代方法是测试您关心的每个条件关于分开。例如,如果在 git status 的输出中有未跟踪的文件,则可能不总是在意。



例如,要查看是否有任何本地暂挂更改,可以查看返回代码:

  git diff --exit -code 

要检查是否有任何更改已暂存但未提交,可以使用return代码:

  git diff --cached --exit-code 

最后,如果您想知道工作树中是否存在未被忽略的未跟踪文件,您可以测试下列命令的输出是空的:

  git ls-files --other --exclude-standard --directory 

更新:下面问您是否可以更改该命令以排除输出中的目录。您可以通过添加 - no-empty-directory 来排除空目录,但要排除该输出中的所有目录,我认为您必须过滤输出,例如:

  git ls-files --other --exclude-standard --directory | egrep -v'/ $'

-v egrep 意味着只输出与模式不匹配的行,并且模式匹配任何以 /


The goal is to get an unambiguous status that can be evaluated in a shell command.

I tried git status but it always returns 0, even if there are items to commit.

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,and now this solve see Mark Longair 's post

I tried this , but cause an problem.

if [ -z $(git status --porcelain) ];
then
    echo "IT IS CLEAN"
else
    echo "PLEASE COMMIT YOUR CHANGE FIRST!!!"
    echo git status
fi

when the

it will get some error , [: ??: binary operator expected

now , I am looking at man and try the 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

解决方案

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.

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:

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 /.

这篇关于如何检查当前分支中是否没有提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆