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)
更新
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 /"
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)
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,它就只能在本地分支上运行.
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)
Dav*_*ura 12
git branch -l <branch-name>
如果分支存在则返回分支名称,如果不存在则什么也不返回
用于脚本中:
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)
在 Windows 批处理脚本上有点不同,
git rev-parse --verify <branch>
if %ERRORLEVEL% == 0 (
echo "Yes"
) else (
echo "No"
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
65871 次 |
| 最近记录: |