有没有更好的方法来确定当地的git分支是否存在? [英] Is there a better way to find out if a local git branch exists?
问题描述
我正在使用以下命令来确定在我的存储库中是否存在带有分支名称
的 local git分支。它是否正确?有没有更好的方法?
请注意,我正在脚本中执行此操作。出于这个原因,如果可能的话,我想远离瓷器命令。
git show-ref --verify --quiet refs / heads /< branch-name>
#$? == 0表示带有<分支名称>的本地分支存在。
更新
git rev-parse --verify< branch-name>
#$? == 0表示名称为< branch-name>的本地分支存在。
据我所知,这是用脚本来做。我不确定还有更多需要补充的内容,但也可能有一个答案只是说该命令可以完成你想要的任何事情:)
唯一可能需要注意的是分支名称中可能包含令人惊讶的字符,因此您可能需要引用<分支名称>
。
I am using the following command to find out if a local git branch with branch-name
exists in my repository. Is this correct? Is there a better way?
Please note that I am doing this inside a script. For this reason I'd like to stay away from porcelain commands if possible.
git show-ref --verify --quiet refs/heads/<branch-name>
# $? == 0 means local branch with <branch-name> exists.
Update
Turns out there is another way. Thanks @jhuynh.
git rev-parse --verify <branch-name>
# $? == 0 means local branch with name <branch-name> exists.
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>
.
这篇关于有没有更好的方法来确定当地的git分支是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!