git 如何列出所有远程引用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42626068/
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
How do I list all remote refs?
提问by x-yuri
Recently I created a PRon GitHub, but some tests were failing. And locally even more tests didn't pass. So I tried to figure out what's the issue.
最近我在 GitHub 上创建了一个PR,但是一些测试失败了。本地甚至更多的测试没有通过。所以我试图找出问题所在。
And the thing I found out is that Travis was testing the repo being at other commitI don't have. The ref is refs/pull/81/merge. So, somebody supposedly merged my branch into the master and created corresponding ref. Was it GitHub or Travis?
我发现 Travis 正在测试我没有的其他提交的 repo 。ref 是 refs/pull/81/merge。因此,据说有人将我的分支合并到 master 并创建了相应的 ref。是 GitHub 还是 Travis?
And the other question, can I list all the refs GitHub repo has?
另一个问题,我可以列出 GitHub 存储库的所有引用吗?
回答by torek
I do not know anything about Travis itself, but GitHub does in fact create these refs/pull/number/merge
refs. In particular, based on observation—the documentation seems a bit skimpy and I am only a somewhat casual user of GitHub—when you click on "new pull request" in GitHub, GitHub:
我对 Travis 本身一无所知,但实际上 GitHub 确实创建了这些引用。特别是,根据观察——文档似乎有点简陋,我只是 GitHub 的一个临时用户——当你在 GitHub 上点击“新的拉取请求”时,GitHub:refs/pull/number/merge
- creates a reference named
refs/pull/number/head
to save the ID of the commit you are asking someone else to merge for you; - attempts to do an automatic merge-and-commit (using
git merge
), and if that succeeds, creates a second reference namedrefs/pull/number/merge
.
- 创建一个名为的引用,以保存您要求其他人为您合并的提交的 ID;
refs/pull/number/head
- 尝试执行自动合并和提交(使用
git merge
),如果成功,则创建名为 的第二个引用。refs/pull/number/merge
Note that this automatically-created merge commit is not on anybranch, it merely has the same pull-request number assigned, and a reference whose name is obvious from the pull-request itself (simply replace head
with merge
). I believe—but have not tested manually—that the first parent of this merge is the tip commit of the branch you are requesting someone be "on" when theydo the samemerge, i.e., the code behind the scenes that creates this refs/pull/number/merge
reference is (or is close enough to):
请注意,这个自动创建的合并提交不在任何分支上,它只是分配了相同的拉取请求编号,以及一个从拉取请求本身可以明显看出其名称的引用(只需替换head
为merge
)。我相信,但没有手动所测试的这个合并的第一个父是尖端提交你的“上”时,要求某人是分支,他们做同样的合并,即创建幕后的这个代码引用(或足够接近):refs/pull/number/merge
commithash=... # the commit ID for the pull-request
mergeinto=$(git rev-parse $branchname) # the branch we are to merge into
prnum=... # get next pull request number
# create the pull request itself
git update-ref refs/pull/$prnum/pull $commithash
# create the merge ref, if the merge succeeds
git checkout $mergeinto
if git merge -m "..." refs/pull/$prnum/pull; then
# merge succeeded: create the merge ref
git update-ref refs/pull/$prnum/merge HEAD
else
# automatic merge failed, discard it
git merge --abort
fi
(this particular code sequence messes about with the index, and leaves HEAD
detached, so it has to be done with the repository locked and post-work cleanup, or using a temporary work-tree; the actual code sequence is probably different for any number of reasons).
(这个特定的代码序列与索引混淆,并保持HEAD
分离,因此必须通过锁定存储库和工作后清理或使用临时工作树来完成;对于任意数量的实际代码序列可能会有所不同原因)。
Hence:
因此:
So, somebody supposedly merged my branch into the master and created corresponding ref. Was it GitHub or Travis?
因此,据说有人将我的分支合并到 master 并创建了相应的 ref。是 GitHub 还是 Travis?
Given that GitHub will, William of Ockhamwould suggest there is no need to invoke Travis. :-)
鉴于 GitHub会,奥卡姆的威廉会建议没有必要调用 Travis。:-)
And the other question, can I list all the refs GitHub repo has?
另一个问题,我可以列出 GitHub 存储库的所有引用吗?
As long as you have access:
只要您可以访问:
$ git ls-remote
(assuming the remote is a GitHub URL) will show all the exposed references. I have not seen a Web interface way to do this, nor found one in my (light) perusal of the GitHub API documentation.
(假设远程是一个 GitHub URL)将显示所有公开的引用。我还没有看到一种 Web 界面方式来做到这一点,也没有在我(轻)细读 GitHub API 文档中找到一种方式。
回答by cmcginty
If you just want a list of all refs in your repo you can run:
如果你只想要你的 repo 中所有 refs 的列表,你可以运行:
git show-ref
git show-ref
This will print the <SHA> <NAME>
of every ref under .git/refs/*
这将打印<SHA> <NAME>
下每个参考的.git/refs/*
回答by Thomas Guyot-Sionnest
One answer mentions git ls-remote
- while this works in general for my use case I needed to list what git thinksthe remote has - for ex. when the server's admin cleans up my stale remote repositories and I want to restore them using my local git clones.
一个答案提到git ls-remote
- 虽然这通常适用于我的用例,但我需要列出 git认为遥控器具有的内容 - 例如。当服务器的管理员清理我过时的远程存储库并且我想使用我的本地 git 克隆恢复它们时。
To list all known remote refs I used to use ls .git/refs/remotes/<name>/
- turns out in some case it's not sufficient, ex on a git-p4
repository one of my remote's refs were only listed in .git/packed-refs
.
要列出我曾经使用过的所有已知远程引用ls .git/refs/remotes/<name>/
- 结果在某些情况下这还不够,例如在git-p4
存储库中,我的远程引用之一仅在.git/packed-refs
.
Using git branch -r
would work but the output is not cleanly parsable and requires grep'ing the desired remote refs.
使用git branch -r
会起作用,但输出不能完全解析并且需要 grep'ing 所需的远程引用。
After a bit of digging I ended up using git for-each-ref
:
经过一番挖掘,我最终使用了git for-each-ref
:
git for-each-ref --format="%(refname)" refs/remotes/<name>/
NB: git show-refs
can also list refs but the pattern is anchored to the end of the full ref name and only full components are matched, so it's only useful if you know what branch name you want or to list all refs and grep them. I wanted something tidy that fits well in a oneliner.
注意:git show-refs
也可以列出 refs,但模式锚定在完整 ref 名称的末尾,并且只匹配完整组件,因此只有当您知道所需的分支名称或列出所有 refs 并 grep 它们时,它才有用。我想要一些整洁的东西,可以很好地放在单衬里。