git - 如何检查给定的远程存储库上是否存在远程分支?

标签 git

如果特定分支存在于给定的远程存储库中,我需要对其进行子树 merge 。问题是远程仓库没有在本地 checkout ,所以无法使用git branch -r。我只有一个远程地址,类似于 https://github.com/project-name/project-name.git。有没有办法仅通过远程地址列出远程分支?我找不到任何有用的东西:(

最佳答案

$ git ls-remote --heads git@github.com:user/repo.git branch-name

如果找到 branch-name,您将得到以下输出:

b523c9000c4df1afbd8371324083fef218669108        refs/heads/branch-name

否则不会发送任何输出。

所以通过管道传输到 wc 会给你 10:

$ git ls-remote --heads git@github.com:user/repo.git branch-name | wc -l

或者,您可以在 git ls-remote 上设置 --exit-code 标志,如果没有匹配的 refs,它将返回退出代码 2被发现。可以直接在 shell 测试中或通过检查状态变量 $? 来检查结果。

$ git ls-remote --exit-code --heads git@github.com:user/repo.git branch-name

关于git - 如何检查给定的远程存储库上是否存在远程分支?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8223906/

相关文章:

git - 使用 SVN 连接到 git 存储库

git - Mercurial 书签和 'Git like branching'

gitignore 以及如何忽略公共(public)目录名称及其内容

svn - 如何将变更集从 hg 存储库导出到 svn 存储库

Git:如何在提交中列出更改的文件,包括每个文件 (blob) 的 SHA-1 哈希值?

git - 比 SVN 或 Git 更喜欢 CVS 的原因

php - 在 Heroku 上部署 Facebook 应用程序

git - 使用 develop 分支 Git 流程开发模型应用修补程序时,如何避免 Git 中的重复提交?

windows - 在 Windows 上使用 SSH 的 Git

Git:如何压缩分支上的所有提交