如果特定分支存在于给定的远程存储库中,我需要对其进行子树合并.问题是远程存储库未在本地签出,因此我无法使用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
如果branch-name发现您将获得以下输出:
b523c9000c4df1afbd8371324083fef218669108        refs/heads/branch-name
否则不会发送任何输出.
如此管道wc将给你1或0:
$ git ls-remote --heads git@github.com:user/repo.git branch-name | wc -l
或者,您可以设置--exit-code标志,如果找不到匹配的引用git ls-remote,将返回退出代码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
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}$'
小智 13
你也可以用这个:
git show-branch remotes/origin/<<remote-branch-name>>
返回最新提交和$的值?是0否则返回"致命:坏sha1引用遥控器/原点/ <>"和$的值?是128
您可以在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
希望能帮助到你.
$ git ls-remote --heads origin <branch> | wc -l
大部分时间都有效.
但如果分支部分匹配如下,则无效
$ git branch -a
creative/dev
qa/dev
$ git ls-remote --heads origin dev | wc -l
2
使用
git ls-remote --heads origin <branch> | \
    cut -d$'\t' -f2 | \
    sed 's,refs/heads/,,' | \
    grep ^<branch>$ | wc -l
如果你想要一个可靠的方式.
如果你想在脚本中使用,并且不想假设origin为默认远程
git ls-remote --heads $(git remote | head -1) "$branch" | \
    cut -d$'\t' -f2 | \
    sed 's,refs/heads/,,' | \
    grep ^"$branch"$ | wc -l
应该管用.
请注意,这git branch -a | grep ...是不可靠的,因为自上次fetch运行以来可能需要一段时间.
然后,无需每次手动传递存储库名称。
git ls-remote origin <branch>
代替
git ls-remote <full repo url> <branch>
范例:
git ls-remote git@bitbucket.org:landmarkgroupme/in-store-application.git  uat_21dec
要么
git ls-remote origin uat_21dec
两者将给出相同的输出:
关于起源的更多信息:Git具有“远程”的概念,它只是存储库其他副本的URL。当您克隆另一个存储库时,Git会自动创建一个名为“ origin”的远程对象并指向它。您可以通过键入git remote show origin来查看有关远程的更多信息。
| 归档时间: | 
 | 
| 查看次数: | 66703 次 | 
| 最近记录: |