如何检查给定远程存储库中是否存在远程分支?

Kee*_*een 87 git

如果特定分支存在于给定的远程存储库中,我需要对其进行子树合并.问题是远程存储库未在本地签出,因此我无法使用git branch -r.我只有一个远程地址,就像这样 https://github.com/project-name/project-name.git.有没有办法只通过远程地址列出远程分支?我找不到任何有用的东西:(

use*_*772 108

$ git ls-remote --heads git@github.com:user/repo.git branch-name
Run Code Online (Sandbox Code Playgroud)

如果branch-name发现您将获得以下输出:

b523c9000c4df1afbd8371324083fef218669108        refs/heads/branch-name
Run Code Online (Sandbox Code Playgroud)

否则不会发送任何输出.

如此管道wc将给你10:

$ git ls-remote --heads git@github.com:user/repo.git branch-name | wc -l
Run Code Online (Sandbox Code Playgroud)

或者,您可以设置--exit-code标志,如果找不到匹配的引用git ls-remote,将返回退出代码2.

  • 注意:如果您不想指定存储库 URL,则可以指定存储库远程名称,例如:`git ls-remote --heads originbranch-name` (7认同)
  • 我使用`git ls-remote --heads $ {REPO} $ {BRANCH} | grep $ {BRANCH}>/dev/null`后跟`if ["$?" =="1"]; 然后回声"分支不存在"; 出口; fi` (6认同)
  • 顺序很重要:“--exit-code”必须位于“--heads”之前。 (6认同)
  • 您需要将分支名称指定为“/refs/heads/branch-name”。否则,即使没有“branch-name”,也会返回分支“foo/branch-name”。 (3认同)
  • `/refs/heads/branch-name` 似乎不起作用,但 `refs/heads/branch-name` 可以。 (2认同)

Dog*_*ert 48

git ls-remote --heads https://github.com/rails/rails.git
5b3f7563ae1b4a7160fda7fe34240d40c5777dcd    refs/heads/1-2-stable
81d828a14c82b882e31612431a56f830bdc1076f    refs/heads/2-0-stable
b5d759fd2848146f7ee7a4c1b1a4be39e2f1a2bc    refs/heads/2-1-stable
c6cb5a5ab00ac9e857e5b2757d2bce6a5ad14b32    refs/heads/2-2-stable
e0774e47302a907319ed974ccf59b8b54d32bbde    refs/heads/2-3-stable
13ad87971cc16ebc5c286b484821e2cb0fc3e3b1    refs/heads/3-0-stable
3df6c73f9edb3a99f0d51d827ef13a439f31743a    refs/heads/3-1-stable
f4db3d72ea564c77d5a689b850751ce510500585    refs/heads/compressor
c5a809e29e9213102351def7e791c3a8a67d7371    refs/heads/deps_refactor
821e15e5f2d9ef2aa43918a16cbd00f40c221e95    refs/heads/encoding
8f57bf207ff4f28fa8da4544ebc573007b65439d    refs/heads/master
c796d695909c8632b4074b7af69a1ef46c68289a    refs/heads/sass-cleanup
afd7140b66e7cb32e1be58d9e44489e6bcbde0dc    refs/heads/serializers
Run Code Online (Sandbox Code Playgroud)

  • 我需要在bash脚本中进行测试,因此只对退出代码感兴趣,所以我在本地克隆中执行了以下操作:`git ls-remote --exit-code.origin/branch-name&>/dev/null`然后使用`$?`作为测试操作数 (7认同)
  • @Darren,只是在条件中直接使用命令,就像`if git ls-remote ...; 然后 ...; fi`,比检查`$?`(可以通过记录语句,陷阱等改变)更不容易出错. (5认同)

Ian*_*emp 21

这里的所有答案都是特定于 Linux shell 的,如果您处于不支持此类操作的环境中(例如 Windows 的命令提示符),这将没有多大帮助。

幸运的是,根据分支是否存在,分别返回 0 或 2git ls-remote--exit-code参数。所以:

git ls-remote --exit-code --heads origin <branch-that-exists-in-origin>

将返回 0,并且

git ls-remote --exit-code --heads origin <branch-that-only-exists-locally>

将返回 2。

对于 PowerShell,您可以简单地使用内置的真实性处理语义:

if (git ls-remote --heads origin <branch-that-exists-in-origin>) { $true } else { $false }

产量$true,而:

if (git ls-remote --heads origin <branch-that-only-exists-locally>) { $true } else { $false }

产量$false


РАВ*_*АВИ 14

如果是要运行的git repo,则可以在当前文件夹中使用的另一种方法

git branch -a | egrep 'remotes/origin/${YOUR_BRANCH_NAME}$'
Run Code Online (Sandbox Code Playgroud)

  • 使用双引号:`git branch -a | egrep"remotes/origin/$ {YOUR_BRANCH_NAME} $"` (2认同)
  • 这不是最佳的,因为即使您只匹配现有分支名称的一部分,但它与目标分支不完全匹配,它也会返回true.所以这不能安全地用在脚本中,以便在检查之前测试分支是否存在.无论如何,感谢分享,因为我发现它很有用.可以像这样修复:`git branch -a | grep"\ b $ {BRANCH} $"` (2认同)
  • 如果使用`git branch -a`,则需要`git fetch`,因此首先获取所有远程引用。否则使用其他人指出的`git ls-remote`。 (2认同)

小智 13

你也可以用这个:

git show-branch remotes/origin/<<remote-branch-name>>
Run Code Online (Sandbox Code Playgroud)

返回最新提交和$的值?是0否则返回"致命:坏sha1引用遥控器/原点/ <>"和$的值?是128

  • 这不会在检查之前从远程提取分支,因此您无法获得最新的远程状态。 (4认同)

sag*_*nms 9

您可以在Bash终端中执行类似的操作.只需用您要执行的命令替换回声即可.

if git ls-remote https://username:password@github.com/project-name/project-name.git | grep -sw "remote_branch_name" 2>&1>/dev/null; then echo "IT EXISTS..START MERGE" ; else echo "NOT FOUND" ; fi
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.

  • 我喜欢。其他答案也有 ls-remote,我喜欢的是“如果”。 (4认同)

hIp*_*pPy 6

$ git ls-remote --heads origin <branch> | wc -l
Run Code Online (Sandbox Code Playgroud)

大部分时间都有效.

但如果分支部分匹配如下,则无效

$ git branch -a
creative/dev
qa/dev

$ git ls-remote --heads origin dev | wc -l
2
Run Code Online (Sandbox Code Playgroud)

使用

git ls-remote --heads origin <branch> | \
    cut -d$'\t' -f2 | \
    sed 's,refs/heads/,,' | \
    grep ^<branch>$ | wc -l
Run Code Online (Sandbox Code Playgroud)

如果你想要一个可靠的方式.

如果你想在脚本中使用,并且不想假设origin为默认远程

git ls-remote --heads $(git remote | head -1) "$branch" | \
    cut -d$'\t' -f2 | \
    sed 's,refs/heads/,,' | \
    grep ^"$branch"$ | wc -l
Run Code Online (Sandbox Code Playgroud)

应该管用.

请注意,这git branch -a | grep ...是不可靠的,因为自上次fetch运行以来可能需要一段时间.


Ami*_*esh 6

然后,无需每次手动传递存储库名称。

git ls-remote origin <branch>
Run Code Online (Sandbox Code Playgroud)

代替

git ls-remote <full repo url> <branch>
Run Code Online (Sandbox Code Playgroud)

范例:

git ls-remote git@bitbucket.org:landmarkgroupme/in-store-application.git  uat_21dec
Run Code Online (Sandbox Code Playgroud)

要么

git ls-remote origin uat_21dec
Run Code Online (Sandbox Code Playgroud)

两者将给出相同的输出:

在此处输入图片说明

关于起源的更多信息:Git具有“远程”的概念,它只是存储库其他副本的URL。当您克隆另一个存储库时,Git会自动创建一个名为“ origin”的远程对象并指向它。您可以通过键入git remote show origin来查看有关远程的更多信息。


ale*_*999 5

将返回名称中包含查询的所有分支(远程或本地)。

git branch --all | grep <query>