合并哪个分支?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (22)

$ git branch -v
  bucket-1       410f7b5 * gh-53 * gh-48 * "Share App"
  bucket-2       7ed70a2 * upgrade to SOLR 3.3.0
  bucket-3       400ffe4 * emergency fix prod issue
* bucket-4       64c2414 Merge branch 'bucket-3' into bucket-4
  master         8dc4854 [ahead 1] * gh-73

我想从我的遥控器里拿出一些变化:

$ git pull

You asked me to pull without telling me which branch you
want to merge with, and 'branch.bucket-4.merge' in
your configuration file does not tell me, either. Please
specify which branch you want to use on the command line and
try again (e.g. 'git pull <repository> <refspec>').
See git-pull(1) for details.

If you often merge with the same branch, you may want to
use something like the following in your configuration file:

    [branch "bucket-4"]
    remote = <nickname>
    merge = <remote-ref>

    [remote "<nickname>"]
    url = <url>
    fetch = <refspec>

See git-config(1) for details.

我以为我已经把“四桶”作为跟踪分支了。让我们看看:

$ git remote show origin
* remote origin
  Fetch URL: git@github.com:abcd/main.git
  Push  URL: git@github.com:abcd/main.git
  HEAD branch (remote HEAD is ambiguous, may be one of the following):
    bucket-3
    master
  Remote branches:
    bucket-1       tracked
    bucket-2       tracked
    bucket-3       tracked
    bucket-4       tracked
    master         tracked
  Local branches configured for 'git pull':
    bucket-1       merges with remote bucket-1
    bucket-2       merges with remote bucket-2
    bucket-3       merges with remote bucket-3
    master         merges with remote master
  Local refs configured for 'git push':
    bucket-1       pushes to bucket-1       (up to date)
    bucket-2       pushes to bucket-2       (up to date)
    bucket-3       pushes to bucket-3       (up to date)
    bucket-4       pushes to bucket-4       (local out of date)
    master         pushes to master         (fast-forwardable)

事实上,桶-4标记为“跟踪”,但不知何故,它被配置为推送,但不是拉。

看着我的.git/config文件中,我看到我的大部分分支都有“远程”和“合并”条目,但没有用于桶-4。如果没有这个,怎么会被认为是“跟踪”呢?

[remote "origin"]
    url = git@github.com:abcd/main.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master
[branch "rel-2011-07-07"]
    remote = origin
    merge = refs/heads/rel-2011-07-07
[branch "bucket-1"]
    remote = origin
    merge = refs/heads/bucket-1
[branch "bucket-2"]
    remote = origin
    merge = refs/heads/bucket-2
[branch]
    autosetupmerge = true
[branch "bucket-3"]
    remote = origin
    merge = refs/heads/bucket-3

我看到这里可能的解决方案是remote/merge在我的配置文件中的桶-4条目。但是如果没有这个,它怎么被认为是“跟踪”的呢?桶-4是在本地创建的,然后从这个回购中推到服务器上,所以我怀疑我没有为这个分支正确地设置跟踪。

我是否可以添加一些配置,以使所有本地分支在将来正确跟踪它们的遥控器?

提问于
用户回答回答于

上面写着bucket-4 pushes to bucket-4只是因为在推送分支时,默认情况是将其推送到远程上具有匹配名称的分支。

最简单的方法是在bucket-4bucket-4origin就是确保下次你推的时候:

git push -u origin bucket-4

或者,你也可以这样做:

git branch --set-upstream bucket-4 origin/bucket-4

直接回答你的几个问题:

如果没有这个,怎么会被认为是“跟踪”呢?

在这种情况下,如果没有远程跟踪分支,它就不会跟踪远程跟踪分支。branch.bucket-4.mergebranch.bucket-4.remote在你的git配置中。输出git remote show origin只是向你展示了默认情况下会把分支推到哪里。

我是否可以添加一些配置,以使所有本地分支在将来正确跟踪它们的遥控器?

我不认为有。当你创造bucket-4在本地,正如我所假设的,远程跟踪分支不存在,因此不能在此时设置它--这将是非常混乱的默认行为。你只需要记住-u敬你的第一次git push它的上游存储库。

用户回答回答于

git branch --set-upstream <branch> origin/<branch>至少在1.8.2.3(我的版本)时被否决。

使用git branch --set-upstream-to=origin/<branch> <branch>相反。

扫码关注云+社区