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

Man*_*dan 169 git git-commands git-branch

我使用以下命令来查明我的存储库中是否存在本地 git分支branch-name.它是否正确?有没有更好的办法?

请注意我在脚本中执行此操作.因此,如果可能的话,我想远离瓷器命令.

git show-ref --verify --quiet refs/heads/<branch-name>
# $? == 0 means local branch with <branch-name> exists. 
Run Code Online (Sandbox Code Playgroud)

更新

事实证明还有另一种方式.谢谢@jhuynh.

git show-ref --verify --quiet refs/heads/<branch-name>
# $? == 0 means local branch with <branch-name> exists. 
Run Code Online (Sandbox Code Playgroud)

小智 101

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

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

git rev-parse --verify <branch_name>
Run Code Online (Sandbox Code Playgroud)

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

  • 只需注意:`git rev-parse --verify`只告诉你repo中是否存在这样的对象(即它将为`<branch_name>`的任何值返回0,转换为任何类型的对象回购).它不会告诉您该对象是否是分支. (17认同)
  • **git rev-parse --verify gh-pages**给了我:致命:需要一个修订 (13认同)
  • 这不是问题的正确答案,问题是如何知道分支是否存在.这会给你一个标签的误报.您可以自己轻松测试.你需要refs/heads /来区分refs/tags中的标签,甚至是refs/remotes中的遥控器. (6认同)
  • 这给了我“致命:也需要一次修订”。 (5认同)
  • 无需使用 `[` ... `]`。只是 `if git rev-parse --verify main &gt;/dev/null 2&gt;&amp;1 ; 然后...`。它使用命令返回的成功/失败状态。(如前所述,它对于分支和标签都返回 true。) (2认同)

Mar*_*air 49

据我所知,这是在脚本中执行此操作的最佳方式.我不确定还有更多内容可以添加,但也可能只有一个答案就是说"那个命令能做你想做的一切":)

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


Mar*_*ijn 30

差不多了.

刚离开了--verify,并--quiet和你要么如果分支存在散列或什么,如果它没有.

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

exists=`git show-ref refs/heads/<branch-name>`
if [ -n "$exists" ]; then
    echo 'branch exists!'
fi
Run Code Online (Sandbox Code Playgroud)

  • 返回值就足够了 - 您不需要经历可能容易出错的分配变量的工作. (6认同)

Mar*_*ago 15

我想你可以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
Run Code Online (Sandbox Code Playgroud)

那么,$?== 0表示分支存在,你不必深入挖掘refs/heads /的管道.只要你没有传递-r给show-branch,它就只能在本地分支上运行.

  • AFAIK`git show-branch`是一个_porcelain_命令.正如我在我的问题中所说,如果可以使用管道等效物,我宁愿不在脚本中使用瓷器命令.见http://www.kernel.org/pub/software/scm/git/docs/ (5认同)
  • @Manoj:我知道瓷器和管道,但我从未读过管道被认为比瓷器更稳定.感谢您在文档中指出我. (3认同)
  • 请注意,它也会接受标签作为分支. (2认同)

Raz*_*ssa 12

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

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

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

脚本中的用法:

if git show-ref --quiet refs/heads/develop; then
    echo develop branch exists
fi
Run Code Online (Sandbox Code Playgroud)

  • 奇怪的是我必须向下滚动三个答案才能获得最佳解决方案。我想这个答案最终会登上顶峰。 (2认同)

Dav*_*ura 12

git branch -l <branch-name>

如果分支存在则返回分支名称,如果不存在则什么也不返回

  • 这是一个更好的解决方案。例如: ```git_checkout_main() { main_branch=$(gitbranch -l main) if [ -z "${main_branch}" ]; 然后 git checkout master else git checkout main fi }``` (2认同)

Tom*_*ale 7

用于脚本中:

git show-ref -q --heads <branch-name>
Run Code Online (Sandbox Code Playgroud)

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

例:

if git show-ref -q --heads <branch-name>; then
   echo 'Branch exists'
fi
Run Code Online (Sandbox Code Playgroud)


pin*_*sia 5

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

git rev-parse --verify <branch>

if %ERRORLEVEL% == 0  (
    echo "Yes"
) else (
    echo "No"
)
Run Code Online (Sandbox Code Playgroud)