获取默认的远程推送和默认的远程分支 [英] Get default remote push and default remote branch
问题描述
我想从脚本中获取默认的推送远程和默认的推送分支.
From a script, I would like to get the default push remote and the default push branch.
为进行召回,git将按以下顺序在这些设置之间选择遥控器:
For the recall, git will choose the remote between these settings, in this order:
-
branch.<name>.pushRemote
-
remote.pushDefault
-
branch.<name>.remote
- 最后一次默认
- 起源
config push.default current
- 如果
config push.default upstream
为NULL
branch.<name>.pushRemote
remote.pushDefault
branch.<name>.remote
- very last default
- origin if
config push.default current
- NULL if
config push.default upstream
我找不到任何参考,git如何选择最后一个默认来源,但似乎是静态的.
I can't find any reference how git is choosing this last default origin, but it seems static.
默认远程分支可以是:
-
branch.<name>.merge
如果config push.default upstream
- 否则为当前分支名称
branch.<name>.merge
ifconfig push.default upstream
- current branch name otherwise
现在,我想获得一种安全的方法来同时获取默认推送远程和默认推送分支.
Now, I would like a safe way to get both default push remote and default push branch.
- 我可以使用
git config --get
,但是我必须根据config push.default
来解决自己的举止,这似乎有点冒险. - 我希望使用以下一种:
-
git for-each-ref --format='%(push:short)' refs/heads/mybranch
-
git rev-parse --abbrev-ref=loose mybranch@{push}
-
- I could use
git config --get
, but I'll have to resolve myself the behoavior according toconfig push.default
, and it seems a bit risky. - I would prefer to use one of these:
git for-each-ref --format='%(push:short)' refs/heads/mybranch
git rev-parse --abbrev-ref=loose mybranch@{push}
最后,我有两个问题:
-
git for-each
和git rev-parse
返回类似origin/mybranch
的路径.如何在远程名称和分支名称之间进行拆分(远程名称可以包含/
.) - 两者之间是否有更安全的功能? (在我所有的测试中,它们始终返回相同的输出).
git for-each
andgit rev-parse
returns a path likeorigin/mybranch
. How can I split between the remote name and the branch name (a remote name can contains/
's).- Is there a safer function between the both? (in all my tests, they always return the same output).
推荐答案
在Git 2.16(2018年第一季度)中,有一个新语法,其中"remote"存储库的名称和远程参考 通过"
%(push:remotename)
"对"upstream
"和"push
"影响的 和朋友.With Git 2.16 (Q1 2018), there is a new syntax, where the "
--format=...
" option for "git for-each-ref
" learned to show the name of the 'remote
' repository and the ref at the remote side that is affected for 'upstream
' and 'push
' via "%(push:remotename)
" and friends.请参见提交1b58686 (2017年11月7日)和 Johannes Schindelin(
dscho
).
请参见提交9700fae (2017年11月7日),作者是 Junio C Hamano-gitster
-在See commit 1b58686 (07 Nov 2017), and commit cc72385 (05 Oct 2017) by Johannes Schindelin (
dscho
).
See commit 9700fae (07 Nov 2017) by J Wyman (whoisj
).
(Merged by Junio C Hamano --gitster
-- in commit 093048b, 15 Nov 2017)for-each-ref
:让上游/推送有选择地报告远程名称有时候脚本不仅要知道远程存储库上上游分支的名称,而且要知道远程的名称.
for-each-ref
: let upstream/push optionally report the remote nameThere are times when e.g. scripts want to know not only the name of the upstream branch on the remote repository, but also the name of the remote.
此补丁为上游和下游版本提供了新的后缀
:remotename
. 推动原子,从而可以准确地表明这一点.示例:This patch offers the new suffix
:remotename
for the upstream and for the push atoms, allowing to show exactly that. Example:$ cat .git/config ... [remote "origin"] url = https://where.do.we.come/from fetch = refs/heads/*:refs/remote/origin/* [remote "hello-world"] url = https://hello.world/git fetch = refs/heads/*:refs/remote/origin/* pushURL = hello.world:git push = refs/heads/*:refs/heads/* [branch "master"] remote = origin pushRemote = hello-world ... $ git for-each-ref \ --format='%(upstream) %(upstream:remotename) %(push:remotename)' \ refs/heads/master refs/remotes/origin/master origin hello-world
该实现选择 not 来 DWIM 远程推送(如果没有明确的话) 推送远程已配置;原因是有可能对此进行DWIM 通过使用
The implementation chooses not to DWIM the push remote if no explicit push remote was configured; The reason is that it is possible to DWIM this by using
%(if)%(push:remotename)%(then) %(push:remotename) %(else) %(upstream:remotename) %(end)
虽然如果出现以下情况,则无法取消DWIM"信息 呼叫者实际上只对显式推送遥控器感兴趣.
while it would be impossible to "un-DWIM" the information in case the caller is really only interested in explicit push remotes.
虽然
:remote
会更短,但也会更加模棱两可, 而且还会关门,例如为:remoteref
(这将 显然是指远程存储库中的相应引用.While
:remote
would be shorter, it would also be a bit more ambiguous, and it would also shut the door e.g. for:remoteref
(which would obviously refer to the corresponding ref in the remote repository).
for-each-ref
:让上游/推送报告远程引用名称有时候脚本不仅要知道 在远程上推送分支,但也知道分支的名称 通过远程存储库.
for-each-ref
: let upstream/push report the remote ref nameThere are times when scripts want to know not only the name of the push branch on the remote, but also the name of the branch as known by the remote repository.
例如,当某个工具想要推送到相同的分支时 它会自动从中拉出,即
<remote>
和<to>
在git push <remote> <from>:<to>
中将由%(upstream:remotename)
和%(upstream:remoteref)
.An example of this is when a tool wants to push to the very same branch from which it would pull automatically, i.e. the
<remote>
and the<to>
ingit push <remote> <from>:<to>
would be provided by%(upstream:remotename)
and%(upstream:remoteref)
, respectively.此修补程序为
upstream
和push
提供了新的后缀:remoteref
. 原子,可以准确地表明这一点.示例:This patch offers the new suffix
:remoteref
for theupstream
andpush
atoms, allowing to show exactly that. Example:$ cat .git/config ... [remote "origin"] url = https://where.do.we.come/from fetch = refs/heads/*:refs/remote/origin/* [branch "master"] remote = origin merge = refs/heads/master [branch "develop/with/topics"] remote = origin merge = refs/heads/develop/with/topics ... $ git for-each-ref \ --format='%(push) %(push:remoteref)' \ refs/heads refs/remotes/origin/master refs/heads/master refs/remotes/origin/develop/with/topics refs/heads/develop/with/topics
这篇关于获取默认的远程推送和默认的远程分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- origin if
- 起源