如何确定Git合并是否正在进行中 [英] How to determine if Git merge is in process

查看:204
本文介绍了如何确定Git合并是否正在进行中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有Git命令可用于确定合并是否处于进程中(即未提交)?我知道我可以简单地检查 .git / MERGE_HEAD ,但是对于命令行脚本来说,这是否正确和/或适用于未来? p>

Is there a Git command that can be used to determine if a merge is in-process (i.e. uncommitted)? I know I can simply check for .git/MERGE_HEAD, but is this proper and/or future-proof for command-line scripts?

推荐答案

一个技巧是使用Git命令,如果正在进行合并,将会失败。然后您可以检查命令的返回码。您还需要确保该命令在成功时不会影响您的工作副本或索引。虽然有很多命令属于这个类别,但看起来合适的命令是

One trick is to use a Git command that will fail if a merge is in progress. You can then check the return code of the command. You'll also want to make sure that the command will not affect your working copy or index in the event that it succeeds. While there are many commands that fall into this category, one that seems appropriate is

git merge HEAD

如果正在进行合并,则返回128代码,否则返回0。请注意,当合并不在进程中时,此命令将仅打印已经最新的,因为您只需合并自己。因此,为了编写脚本的目的,你可以这样做(在BASH中)

which returns a 128 code if a merge is in progress, but 0 otherwise. Note that when a merge is not in-process, this command will simply print Already up-to-date since you are just merging with yourself. So, for scripting purposes, you can do this (in BASH)

git merge HEAD &> /dev/null
result=$?
if [ $result -ne 0 ]
then
    echo "Merge in progress."
else
    echo "Merge not in progress."
fi

请注意,即使使用 - quiet git merge 标志,一个进程内合并仍然会导致这个命令打印到错误流中。这就是为什么我将它的输出重定向到 / dev / null

Note that even with the --quiet flag of git merge, an in-process merge will still cause this command to print to the error stream. This is why I redirect its output to /dev/null.

这篇关于如何确定Git合并是否正在进行中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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