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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8223906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:23:06  来源:igfitidea点击:

How to check if remote branch exists on a given remote repository?

git

提问by Keen

I need to do a subtree merge for a specific branch, if it exists on a given remote repository. The problem is that the remote repository is not checked out locally, so I can't use git branch -r. All I have is a remote address, something like this https://github.com/project-name/project-name.git. Is there a way to list remote branches just by a remote address? I couldn't find anything usefull :(

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

回答by user487772

$ git ls-remote --heads [email protected]:user/repo.git branch-name

In case branch-nameis found you will get the following output:

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

b523c9000c4df1afbd8371324083fef218669108        refs/heads/branch-name

Otherwise no output will be sent.

否则不会发送任何输出。

So piping it to wcwill give you 1or 0:

所以管道它wc会给你10

$ git ls-remote --heads [email protected]:user/repo.git branch-name | wc -l

Alternatively you can set --exit-codeflag on git ls-remotewhich will return exit code 2in case no matching refs are found.

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

回答by Dogbert

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

回答by РАВИ

Another way you can use in the current folder if it is a git repoto run

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

git branch -a | egrep 'remotes/origin/${YOUR_BRANCH_NAME}$'

回答by Ajo Paul

You can also use this:

你也可以使用这个:

git show-branch remotes/origin/<<remote-branch-name>>

returns latest commit and value of $? is 0 otherwise returns "fatal: bad sha1 reference remotes/origin/<>" and value of $? is 128

返回最新提交和 $? 是 0 否则返回“fatal: bad sha1 reference remotes/origin/<>”和$的值?是 128

回答by Amitesh

Then no need to manually pass the repository name everytime.

这样就无需每次都手动传递存储库名称。

git ls-remote origin <branch>

Instead of

代替

git ls-remote <full repo url> <branch>

Example :

例子 :

git ls-remote [email protected]:landmarkgroupme/in-store-application.git  uat_21dec

OR

或者

git ls-remote origin uat_21dec

Both will give same output :

两者都会给出相同的输出:

enter image description here

在此处输入图片说明

More on Origin: Git has the concept of "remotes", which are simply URLs to other copies of your repository. When you clone another repository, Git automatically creates a remote named "origin" and points to it. You can see more information about the remote by typing git remote show origin .

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

回答by sagunms

You can do something like this in the Bash terminal. Just replace the echos with the commands you want to execute.

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

if git ls-remote https://username:[email protected]/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

Hope it helps.

希望能帮助到你。

回答by Ian Kemp

All of the answers here are Linux shell-specific, which doesn't help very much if you're in an environment that doesn't support those sort of operations - for example, Windows' command prompt.

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

Fortunately git ls-remoteaccepts an --exit-codeargument that returns 0 or 2 depending on whether the branch exists or not, respectively. So:

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

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

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

will return 0, and

将返回 0,并且

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

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

will return 2.

将返回 2。

For PowerShell, you can simply use the built-in truthiness handling semantics:

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

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

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

yields $true, while:

产量$true,而:

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

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

yields $false.

产量$false

回答by hIpPy

$ git ls-remote --heads origin <branch> | wc -l

works most of the time.

大部分时间都在工作。

But will not work if branch matches partially as below,

但如果分支部分匹配如下,则不起作用,

$ git branch -a
creative/dev
qa/dev

$ git ls-remote --heads origin dev | wc -l
2

Use

git ls-remote --heads origin <branch> | \
    cut -d$'\t' -f2 | \
    sed 's,refs/heads/,,' | \
    grep ^<branch>$ | wc -l

if you want a reliable way.

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

If you want to use in script and do not want to assume originas default remote then

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

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

should work.

应该管用。

Note that git branch -a | grep ...is not reliable as it may be a while since the last fetchwas run.

请注意,这git branch -a | grep ...并不可靠,因为距离上次fetch运行可能已经有一段时间了。

回答by Noufal Ibrahim

You can add the repository you have as a remote using git remote add something https://github.com/project-name/project-name.gitand then do a git remote show somethingto get all information about the remote. This requires a network connection and is useful for human use.

您可以将您拥有的存储库添加为远程使用git remote add something https://github.com/project-name/project-name.git,然后执行 agit remote show something以获取有关远程的所有信息。这需要网络连接,对人类使用很有用。

Alternatively, do a git fetch something. This will fetch all branches on the remote called somethingand keep them in your local repository. You can then merge them into your local branch as you please. I recommend this path since if you finally decide that you haveto merge, this is what you need to do.

或者,执行一个git fetch something. 这将获取远程调用上的所有分支something并将它们保存在您的本地存储库中。然后,您可以根据需要将它们合并到您的本地分支中。我推荐这条路径,因为如果您最终决定必须合并,这就是您需要做的。

OT: Your use of "checked out locally" indicates that you're approaching this from a centralised version control system standpoint. That's usually a dead end when you're dealing with git. It uses words like "checkout" etc. differently from how older systems did.

OT:您对“本地签出”的使用表明您是从集中式版本控制系统的角度来解决这个问题的。当您处理 git 时,这通常是一个死胡同。它使用“结帐”等词,与旧系统的使用方式不同。

回答by nos

You can try

你可以试试

git diff --quiet @{u} @{0}

Here @{u}refers to remote/upstream, and @{0}refers to current local HEAD (with newer version of git, @{0}can be shortened as @). If remote does not exist, it errors out.

这里@{u}指的是远程/上游,@{0}指的是当前本地的 HEAD(在 git 版本较新的情况下,@{0}可以缩写为@)。如果 remote 不存在,则会出错。

With git 2.16.2 (I am not sure which version is the first to have this functionality, for example, git 1.7.1 doesn't have it), you can do

使用 git 2.16.2(我不确定哪个版本是第一个具有此功能的版本,例如 git 1.7.1 没有),您可以这样做

git checkout

If a remote branch exists, there will be some output, like

如果存在远程分支,则会有一些输出,例如

Your branch is up to date with 'origin/master'

Otherwise there is no output.

否则没有输出。