git 你让我拉,但没有告诉我你想和哪个分支合并

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7388278/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:49:58  来源:igfitidea点击:

You asked me to pull without telling me which branch you want to merge with

gitgit-pull

提问by George Armhold

TL;DR: I have a "tracked" branch that I can't pull.

TL;DR:我有一个无法拉动的“跟踪”分支。

So here I am in "bucket-4":

所以我在“bucket-4”中:

$ 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

I'd like to pull in changes from my remote:

我想从我的遥控器中提取更改:

$ 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.

Hmm, odd, I thought I already added "bucket-4" as a tracking branch. Let's see:

嗯,奇怪,我以为我已经添加了“bucket-4”作为跟踪分支。让我们来看看:

$ git remote show origin
* remote origin
  Fetch URL: [email protected]:abcd/main.git
  Push  URL: [email protected]: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)

Indeed, bucket-4 is marked as "tracked", yet somehow it's configured for push, but not pull.

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

Looking at my .git/configfile, I see that I have "remote" and "merge" entries for most of my branches, but not for bucket-4. How is it even considered "tracked" without this?

查看我的.git/config文件,我发现我的大多数分支都有“远程”和“合并”条目,但对于bucket-4 则没有。如果没有这个,它如何被认为是“跟踪”的?

[remote "origin"]
    url = [email protected]: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

I see that the likely solution here is to add remote/mergeentries for bucket-4 in my config file. But how is it considered "tracked" without this? bucket-4 was created locally, then pushed to the server from this repo, so I suspect that somehow I didn't set up tracking properly for this branch.

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

Is there some configuration I can add in order to make all local branches track their remotes properly in the future?

我是否可以添加一些配置,以便将来所有本地分支机构都能正确跟踪其遥控器?

回答by Mark Longair

It says bucket-4 pushes to bucket-4just because the default when pushing a branch is to push it to one with a matching name on the remote. (Note that this is still the default, even ifthe local branch is tracking a remote-tracking branch and the remote-tracking branch corresponds to a branch with a different name in the remote repository.)

它说bucket-4 pushes to bucket-4只是因为推送分支时的默认设置是将它推送到远程上具有匹配名称的分支。(请注意,这仍然是默认值,即使本地分支正在跟踪远程跟踪分支并且远程跟踪分支对应于远程存储库中具有不同名称的分支。)

The simplest way to set up the association between your bucket-4and bucket-4in originis to make sure that the next time you push, you do:

设置 yourbucket-4bucket-4in之间关联的最简单方法origin是确保下次推送时,您执行以下操作:

git push -u origin bucket-4

Alternatively, you can do:

或者,您可以执行以下操作:

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


To answer a couple of your questions directly:

要直接回答您的几个问题:

How is it even considered "tracked" without this?

如果没有这个,它如何被认为是“跟踪”的?

In this case it isn't - it's not tracking the remote-tracking branch in any sense if there's no branch.bucket-4.mergeor branch.bucket-4.remotein your git config. The output from git remote show originis just showing you where the branch would be pushed by default.

在这种情况下,它不是 - 如果您的 git 配置中没有branch.bucket-4.mergebranch.bucket-4.remote在任何意义上,它都不会跟踪远程跟踪分支。来自的输出git remote show origin只是向您显示默认情况下将推送分支的位置。

Is there some configuration I can add in order to make all local branches track their remotes properly in the future?

我是否可以添加一些配置,以便将来所有本地分支机构都能正确跟踪其遥控器?

I don't think that there is. When you created bucket-4locally, as I assume happened, the remote-tracking branch didn't exist, so it can't be set up at that point - it would be very confusing default behaviour. You just need to remember to add -uto your first git pushof that branch to its upstream repository.

我不认为有。当您在bucket-4本地创建时,正如我假设发生的那样,远程跟踪分支不存在,因此此时无法设置它 - 这将是非常令人困惑的默认行为。您只需要记住将该分支-u的第一个添加git push到其上游存储库。

I hope that's of some help.

我希望这会有所帮助。

回答by d_roge

git branch --set-upstream <branch> origin/<branch>was deprecated at least as of 1.8.2.3 (my version).

git branch --set-upstream <branch> origin/<branch>至少从 1.8.2.3(我的版本)开始被弃用。

Use git branch --set-upstream-to=origin/<branch> <branch>instead.

使用git branch --set-upstream-to=origin/<branch> <branch>来代替。