git - 为 pull 的非默认远程指定默认分支

标签 git github

我将远程源设置为我当前分支的默认分支。我还有一个上游 Remote ,它不是分支的默认设置。有没有办法在 Remote 上配置一个默认分支,这样当我 pull 它时默认到那个分支?

这是我的 .git/config:

[remote "origin"]
        fetch = +refs/heads/*:refs/remotes/origin/*
        url = git@github.com:studgeek/knockout.git
[branch "gh-pages"]
        remote = origin
        merge = refs/heads/gh-pages
[remote "upstream"]
        url = git://github.com/SteveSanderson/knockout.git
        fetch = +refs/heads/*:refs/remotes/upstream/*
        merge = refs/heads/gh-pages

有了它,我可以愉快地执行以下操作,它默认为 origin/gh-pages

git pull

我想做的就是给它远程上游并让它找出分支(gh-pages)部分

git pull upstream

而不是这个

git pull upstream gh-pages

如果我省略分支,现在我会得到以下结果:

$ git pull upstream
You asked to pull from the remote 'upstream', but did not specify
a branch. Because this is not the default configured remote
for your current branch, you must specify a branch on the command line.

我可以看到三种不同的违约方式,它们在我目前的情况下对我有用,但我不确定如何去做:) * 只使用当前分支作为远程上游的默认分支 * 指示当前分支的上游远程的默认分支(同时将 origin 保留为默认分支) * 指示远程上的默认分支。这里的危险当然是如果我切换分支默认的上游分支保持不变。就我而言,这很好,但我可以看到那些没有预料到的人。

备注specifying git branch for remote问一个类似的问题,但解决方案需要做我们不想做的两件事之一 - 更改默认 Remote 或明确列出分支(我们希望将其编码以避免手动错误)。

最佳答案

在 .git/config 中,您可以提供信息。例如,如果你在分支 foo 中并且你想从 moo

中远程 pull
[branch "foo"]
   remote = origin
   merge = refs/heads/moo

下次您在分支 foo 中运行 git pull 时,它将从 moo pull 。

这已在 stackoverflow 中介绍 => How do you get git to always pull from a specific branch?

关于git - 为 pull 的非默认远程指定默认分支,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7456709/

相关文章:

git - "No remote for the current branch"是什么意思?

Git CMD : Getting HTTP Basic: Access denied issue when trying to clone the project from GitLab

使用 Puppet & Hiera 的 Git 工作流

java - 更改圆弧半径

git clone 工作但在替换防火墙后面的 SSL 证书后推送不工作

php - 我应该为每个项目下载 Laravel 吗?

git - Cherry-pick 一个 pull request 到一个分支?

django - 一个项目,多个客户使用 git?

git - 当它们可能有轻微冲突时如何在 GitHub 中提交多个 pull 请求

git - 使用 Github 网络界面或桌面应用程序将 Gist 重置或恢复到较旧的提交状态