git:检查是否在远程仓库中提交 xyz?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5549479/
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
git: check if commit xyz in remote repo?
提问by Jonas Bystr?m
I have a commit xyz in my local branch that I want to check if it is included in a remote release repository; can I do that in some easy way? I could clone the remote repo, but I'm hoping for a nicer+faster way. git ls-remote
seemed promising, but found nothing of value to me there. Thanks!
我在本地分支中有一个提交 xyz,我想检查它是否包含在远程发布存储库中;我可以用一些简单的方式做到这一点吗?我可以克隆远程仓库,但我希望有一种更好+更快的方式。git ls-remote
看起来很有希望,但在那里没有发现对我有价值的东西。谢谢!
回答by Mark Longair
Let's suppose that the remote that refers to the remote repository is called origin
. In that case, first update all your remote-tracking branches with:
假设引用远程存储库的远程称为origin
。在这种情况下,首先使用以下命令更新所有远程跟踪分支:
git fetch origin
Now you can use the useful --contains
option to git branch
to find out which of the remote branches contains that commit:
现在您可以使用有用的--contains
选项来git branch
找出哪个远程分支包含该提交:
git branch -r --contains xyz
(The -r
means to only show remote-tracking branches.) If the commit xyz
is contained in one or more of your remote-tracking branches, you'll see output like:
(-r
仅显示远程跟踪分支的方法。)如果提交xyz
包含在一个或多个远程跟踪分支中,您将看到如下输出:
origin/test-suite
origin/HEAD -> origin/master
origin/master
If it's contained in your local repository, but not one of the remote-tracking branches, the output will be empty. However, if that commit isn't known in your repository at all,you'll get the error malformed object name
and a usage message - perhaps a bit confusing if you're not expecting it...
如果它包含在您的本地存储库中,但不是远程跟踪分支之一,则输出将为空。但是,如果您的存储库中根本不知道该提交,您将收到错误malformed object name
和使用消息 - 如果您不期望它可能会有点混乱......
回答by sehe
Like Mark said,
就像马克说的,
git branch -a --contains commitish
However, beware for branches that contain a cherry-picked/rebased/merged version of the commit.
但是,请注意包含精心挑选/重新定位/合并版本的提交的分支。
This could come in handy
这可以派上用场
git log --cherry-pick --left-right <commitish> ^remote/branchname
It will list the commit ONLYif it doesn't exist (as a cherrypick) in the remote branch. See the man page for log for an explanation on how --cherry-pick identifies equivalent commits
仅当远程分支中不存在(作为cherrypick)提交时,它才会列出提交。有关 --cherry-pick 如何识别等效提交的说明,请参阅日志的手册页
Of course merges/rebases with conflict resolutions or squashes cannot be automatically detected like this
当然,不能像这样自动检测到具有冲突解决方案或挤压的合并/变基
回答by TrentP
The existing answers require the entire remote repository to be downloaded locally. If the remote has many commits that are not yet cloned locally, this could take a very long time. An example is something like the linux-stable repository, which has many independent branches that are never merged. Someone tracking astable kernel might clone only the single branch for that kernel. Needing to fetch all the branches, for every stable series kernel, to see if the commit exists would require downloading much more data.
现有答案需要在本地下载整个远程存储库。如果远程有许多尚未在本地克隆的提交,这可能需要很长时间。一个例子是类似于 linux-stable 存储库,它有许多从不合并的独立分支。有人跟踪一个稳定的内核可能克隆只有单支为内核。需要为每个稳定系列内核获取所有分支以查看提交是否存在将需要下载更多数据。
There does not appear to be a good way to do this without fetching the entire remote repo. The ability is there, based on the way git fetch-pack
and git send-pack
work, but there doesn't seem to be a way to use it in the way desired.
如果不获取整个远程存储库,似乎没有什么好方法可以做到这一点。根据方式git fetch-pack
和git send-pack
工作,能力就在那里,但似乎没有办法以所需的方式使用它。
Pushing a branch to a remote repository does not upload commits the remote already has, and this is done without downloading the entire remote repository first. Trying to fetch a remote commit doesn't need to download the entire remote repository to determine if the requested commit exists or not.
将分支推送到远程存储库不会上传远程已拥有的提交,并且无需先下载整个远程存储库即可完成此操作。尝试获取远程提交不需要下载整个远程存储库来确定请求的提交是否存在。
The latter can be used to achieve what was asked for in some cases.
后者可用于实现某些情况下的要求。
git fetch origin <commit ID>
If the remote does not have that commit, then this fails. The remote repository does not need to be cloned locally to do that. If it the remote does have the commit, then it fetches it. There's no option just see if the fetch would work but not fetch anything. Of course, if the commit is already available locally, then nothing needs to be fetched, and this is not a costly operation.
如果远程没有该提交,则此操作失败。不需要在本地克隆远程存储库来执行此操作。如果远程确实有提交,那么它会获取它。没有选择只是查看获取是否有效但不获取任何内容。当然,如果提交在本地已经可用,那么什么都不需要获取,这不是一个代价高昂的操作。
Some remote repositories will not allow something that is not the head of a branch or a tag to be requested. In that case this won't work.
一些远程存储库不允许请求不是分支头或标签的东西。在这种情况下,这是行不通的。