【问题标题】:Does a Git a push configuration allow specifying the branch as well?Git 推送配置是否也允许指定分支?
【发布时间】:2015-05-20 16:44:29
【问题描述】:

我有一个分支,它从一个遥控器拉出并推送到另一个遥控器,U 使用 git branch --set-upstream-to=xxxx xxxx 设置拉取 repo 和 git config remote.origin.pushurl user@user.com:repo.git 设置推送 repo。

虽然拉取来自源代码库上的master 分支,但push 转到目标代码库上的upstream 分支。

当我切换到分支并执行git push 时,我会收到通常的--global push.default 消息:

warning: push.default is unset; its implicit value has changed in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the traditional behavior, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.

有没有办法为特定分支指定它应该推送到远程的哪个分支,而不是 push.default 值,并且对于拉取也是如此?

【问题讨论】:

  • 你的意思是推送到不同仓库的同名分支吗?
  • @AndrewC No. 这个想法是推送到任何与分支名称不同的任意命名的分支
  • 答案不就是这样吗?

标签: git git-push git-pull git-refspec


【解决方案1】:

这个想法是推送到任何与分支名称不同的任意命名的分支

只需指定一个upstream branch

git branch --set-upstream-to my_local_branch origin/my_remote_branch

那么后续推送就会知道my_local_branch推送到哪个分支。

您仍然需要设置推送策略 (git config push.default simple)

要推送到一个 repo,但从另一个 repo 拉取,您可以将 pushurl 设置为与 pull/fetch url 不同

git config remote.origin.pushurl /url/for/origin/repo
git config remote.origin.url /url/for/upstream/repo

这将允许使用“一个”远程管理所有内容(实际上引用两个不同的存储库)

您还可以更新上游分支部分的 refspecs:

git config remote.origin.push refs/heads/my_local_branch:refs/heads/my_remote_branch
git config remote.origin.fetch refs/heads/my_local_branch:refs/heads/my_local_branch

【讨论】:

  • 我还没有真正玩过这个,只是阅读手册页。您仍然需要为matchingsimple 提供本地分支名称,不是吗(意味着没有引用的git push 不够用)?
  • 从中提取的 repo 与推送到的 repo 不同。 --set-upstream-to 指定拉取的分支,分支名称不同。拉取来自源 repo 上的 master,但来自目标 repo 上的 upstreamupstream repo 永远不会被推送到。我想编辑评论,但它超时了
  • 您使用remote.origin.pushurl 设置的最新答案就是我要询问的内容。该配置语法是否也允许指定分支,即git config remote.origin.pushurl /url/for/origin/repo/<branch>,以便git push 工作,即使--global push.default 尚未设置?
  • @vfclists 分支是由 push refspec 设置的,而不是 push url。请参阅 git-scm.com/docs/git-push#REMOTESlongair.net/blog/2011/02/27/…:“refspecs”通常采用 <src>:<dst> 的形式,告诉您要使用哪个本地分支 (src) 更新远程分支 (def)。但是,如果您不添加:<dst>,则默认行为(如本例中)解释为如果省略:<dst>,则将更新与<src> 相同的引用。
  • 在我的实例中,分支从源代码库中的主分支拉出并推送到目标中的上游分支。这会导致像这样:` [remote "upstream"]` ` url = github.com/sourceuser/repo.git` ` fetch = +refs/heads/*:refs/remotes/upstream/*` ` tagopt = --tags` ` pushurl = github.com/destuser/repo.git` ` push = +refs/heads/upstream:refs/remotes/upstream`
猜你喜欢
  • 2013-10-09
  • 2021-09-08
  • 2021-06-11
  • 2020-10-14
  • 2021-05-31
  • 2011-05-05
  • 2016-06-06
  • 2011-04-05
  • 2017-03-30
相关资源
最近更新 更多