有没有更好的方法来确定本地 git 分支是否存在?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5167957/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:15:07  来源:igfitidea点击:

Is there a better way to find out if a local git branch exists?

gitgit-branchgit-commands

提问by Manoj Govindan

I am using the following command to find out if a localgit branch with branch-nameexists in my repository. Is this correct? Is there a better way?

我正在使用以下命令来确定我的存储库中是否存在本地git 分支branch-name。这样对吗?有没有更好的办法?

Please note that I am doing this inside a script. For this reason I'd like to use plumbing commandsif possible.

请注意,我在脚本中执行此操作。出于这个原因,如果可能的话,我想使用管道命令

git show-ref --verify --quiet refs/heads/<branch-name>
# $? == 0 means local branch with <branch-name> exists. 

采纳答案by Mark Longair

As far as I know, that's the best way to do it in a script. I'm not sure there's much more to add to that, but there might as well be one answer that just says "That command does everything you want" :)

据我所知,这是在脚本中执行此操作的最佳方法。我不确定还有更多要添加的内容,但也可能有一个答案只是说“该命令可以执行您想要的所有操作”:)

The only thing you might want to be careful of is that branch names can have surprising characters in them, so you may want to quote <branch-name>.

您可能唯一需要注意的是分支名称中可能包含令人惊讶的字符,因此您可能需要引用<branch-name>.

回答by jhuynh

When I search for 'git check if branch exists' on a search engine, this page is the first one I see.

当我在搜索引擎上搜索 'git check if branch exists' 时,这个页面是我看到的第一个页面。

I get what I want, but I'd like to provide a updated answer since the original post was from 2011.

我得到了我想要的,但我想提供一个更新的答案,因为原始帖子是 2011 年的。

git rev-parse --verify <branch_name>

This is essentially the same as the accepted answer, but you don't need type in "refs/heads/"

这与接受的答案基本相同,但您不需要输入“refs/heads/”

回答by Martijn

Almost there.

差不多好了。

Just leave out the --verifyand --quietand you get either the hash if the branch exists or nothing if it doesn't.

只需省略--verifyand --quiet,如果分支存在,您将获得散列,如果不存在,您将获得任何散列。

Assign it to a variable and check for an empty string.

将其分配给变量并检查空字符串。

exists=`git show-ref refs/heads/<branch-name>`
if [ -n "$exists" ]; then
    echo 'branch exists!'
fi

回答by Mark Drago

I think you can use git show-branchhere.

我想你可以git show-branch在这里使用。

$ git show-branch --list
  [master] test
* [testbranch] test
$ git show-branch testbranch
[testbranch] test
$ echo $?
0
$ git show-branch nonexistantbranch
fatal: bad sha1 reference nonexistantbranch
$ echo $?
128

So, $? == 0 would indicate that the branch exists and you don't have to dig in to the plumbing of refs/heads/ at all. As long as you don't pass -rto show-branch, it will only operate on local branches.

那么,$? == 0 表示分支存在,您根本不必深入研究 refs/heads/ 的管道。只要不传给-rshow-branch,它只会在本地分支上运行。

回答by Razzi Abuissa

I recommend git show-ref --quiet refs/heads/$name.

我推荐git show-ref --quiet refs/heads/$name

  • --quietmeans there is no output, which is good because then you can cleanly check exit status.

  • refs/heads/$namelimits to local branches and matches full names (otherwise devwould match develop)

  • --quiet意味着没有输出,这很好,因为这样您就可以干净地检查退出状态。

  • refs/heads/$name限制本地分支并匹配全名(否则dev会匹配develop

Usage in a script:

在脚本中的用法:

if git show-ref --quiet refs/heads/develop; then
    echo develop branch exists
fi

回答by Tom Hale

For use in a script:

在脚本中使用:

git show-ref -q --heads <branch-name>

This will exit 0if and only if <branch-name>exists as a local branch.

0当且仅当<branch-name>作为本地分支存在时才会退出。

Example:

例子:

if git show-ref -q --heads <branch-name>; then
   echo 'Branch exists'
fi

回答by Tom Hale

Yup, there is one.

是的,有一个。

git rev-parse [<options>] <args>…?

See https://git-scm.com/docs/git-rev-parsewhere you can find the set of arguments and the function.

请参阅https://git-scm.com/docs/git-rev-parse,您可以在其中找到参数集和函数。

git rev-parse --verify <branch-name>

回答by pinkal vansia

On windows batch script it is bit different,

在 Windows 批处理脚本上有点不同,

git rev-parse --verify <branch>

if %ERRORLEVEL% == 0  (
    echo "Yes"
) else (
    echo "No"
)

回答by Vojtech Vitek

git show-branch <BRANCH-NAME> &>/dev/null && echo yes || echo no

回答by Vojtech Vitek

Let's call it git is_localbranch(you need to add alias in .gitconfig).

让我们调用它git is_localbranch(您需要在 中添加别名.gitconfig)。

Usage:

用法:

$ git is_localbranch BRANCH

Source:

来源:

git branch | grep -w  > /dev/null
if [ $? = 0 ]
then
  echo "branch exists"
fi