分公司不断追踪主人 [英] Branch keeps tracking master

查看:291
本文介绍了分公司不断追踪主人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近开始,我的分支现在默认为 跟踪并推送到远程主服务器,即使我做了 git push -u origin branch_name 即使我使用匹配作为我的 push.default 选项。

 #-------- 
#步骤1
#--------
$ git checkout - b my_branch
分支my_branch设置为跟踪本地分支主机。
转换为新分支'my_branch'

#----------------------------- -------------------------------------------------- -
#第2步:(与当前/上游/简单相同的行为)
#------------------------- -------------------------------------------------- -----
$ git config --global push.default匹配

#--------
#第3步:
# - -------
$ git push -u origin my_branch
计数对象:8,完成。
使用多达8个线程的增量压缩。
压缩对象:100%(8/8),完成。
写作对象:100%(8/8),1.16 KiB | 0字节/秒,完成。
总计8(增量7),重用0(增量0)
到XX:YYY / my_repo
62d390c..4e4baa1 my_branch - > master
分支my_branch设置为从原点跟踪远程分支主机。






另外,如果我为 STEP 2 ,我得到一个错误:

  $ git branch --set- upstream-to = origin / my_branch 
错误:请求的上游分支'origin / my_branch'不存在
hint:
提示:如果您计划将工作基于上游
提示:已经存在于远程的分支,你可能需要
提示:运行git fetch来检索它。
提示:
提示:如果您计划推出一个新的本地分支,
hint:将跟踪其远程对应分支,则可能需要使用
hint:git push -u在您推送时设置上游配置。

我知道我可以编辑 .git / config 手动,但为什么不符合上述规定?






  $ git remote -v 
原点XX:YYY / my_repo(获取)
原点XX:YYY / my_repo(推)

$ git --version
git version 2.9.2


解决方案

作为中间线不应该发生:

lockquote
$ $ $ $ $ $ $ $ git checkout -b my_branch
分支my_branch设置为跟踪本地分支主机。
转换为新分行'my_branch'


例如,当我在存储库中执行 git checkout -b foo 时:

  $ git checkout -b foo 
转换到新分支'foo'

请注意缺乏上游设置。 我的猜测是你的 gco 别名或者脚本里面有 - track >。 s> 编辑:原来是因为您已将 branch.autoSetupMerge 配置为始终,所以从本地分公司创建的新分支跟踪他们的本地分支。



这只是问题的一半,但如果没有发生,完整的问题不会发生无论如何发生。因此,一个修正是删除该设置(默认情况似乎是大多数人最想要的)。




解决另一半问题:
$ b


  $ git branch --set- upstream-to = origin / my_branch 
错误:请求的上游分支'origin / my_branch'不存在




这里的问题实际上就是Git所说的: origin / my_branch 不存在。那么它不存在 :你需要说服你的Git有一个 origin / my_branch 。 Git是Git,有很多方法可以做到这一点,但可能是一种最好的方式,我们马上就会得到。


  $ git push -u origin my_branch 
计数对象:8,完成。
使用多达8个线程的增量压缩。
压缩对象:100%(8/8),完成。
写作对象:100%(8/8),1.16 KiB | 0字节/秒,完成。
总计8(增量7),重用0(增量0)
到XX:YYY / my_repo
62d390c..4e4baa1 my_branch - > master
分支my_branch设置为从原点跟踪远程分支主机。


这是出错的地方:因为 my_branch 已经有一个上游设置 master (不是 origin / master ,只是 master ),你的Git会要求其他Git使用名称 master ,就好像你运行:

  git push origin my_branch:master 

由于这个 git push 没有其他问题,所以他们的方面做你的一方的要求 - 设置他们的 master 与你的 my_branch 相匹配 - 然后你的一方按照 -u ,即将上游设置从 master 更改为 origin / master 。当然,这不是你要的。



(和:你的Git用<$ c替代它似乎有点邪恶$ c> master ,当它是 local master 时,即 branch.my_branch.remote 只是,而不是 origin ,但假设我们无法更改Git本身。 ..)



我看到三个简单的方法可以解决这个问题:


  1. 使用显式的远程端名称: git push -u origin my_branch:my_branch 。这会覆盖当前的上游设置,这样Git会让他们的Git写入 my_branch 。如果成功了,它应该 - 你的Git现在将有 origin / my_branch ,并且会将 my_branch 的上游设置更改为 origin / my_branch



    这个缺点是,如果推送失败,你的Git不会改变当前上游设置。当然,这对于 git push -u (包括接下来的两种方法都是如此,但在这两种情况下,未来没有陷阱)。在运行 git push 之前显式地删除当前上游设置:

  2. code> git branch --unset-upstream my_branch
    (或任何相当的东西,包括编辑 .git / config 文件)。现在没有上游,你的Git不会要求他们的Git使用名称 master



    一旦你的Git要求他们的Git在 origin <上面创建 my_branch (而不是 master ,我们回头看看第一种方法会发生什么。 避免设置本地的 master code>作为第一位的上游。

一种 hard 方法可以做到这一点。伪装你的Git:你可以创建一个远程追踪分支而不需要真正去远程。要做到这一点,你必须使用plumbing命令 git update-ref ,而不是 git branch 。但是,如果你这样做,你可以在实际创建远程分支的推送之前使用 - set-upstream-to 。 (另一个难题是直接编辑 .git / config 或者使用 git config >。这允许你将上游设置为一些实际上还不存在的东西,因为所有的Git在这里真正在做的是存储名称。)


Starting recently, my branches are now by default tracking and pushing to the remote master, even if I do git push -u origin branch_name and even if I use matching as my push.default option.

# --------
# STEP 1
# --------
$ git checkout -b my_branch
Branch my_branch set up to track local branch master.
Switched to a new branch 'my_branch'

# --------------------------------------------------------------------------------
# STEP 2: (same behavior BTW with current/upstream/simple)
# --------------------------------------------------------------------------------
$ git config --global push.default matching

# --------
# STEP 3:
# --------
$ git push -u origin my_branch
Counting objects: 8, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (8/8), 1.16 KiB | 0 bytes/s, done.
Total 8 (delta 7), reused 0 (delta 0)
To XX:YYY/my_repo
   62d390c..4e4baa1  my_branch -> master
Branch my_branch set up to track remote branch master from origin.


Also, if I try the following for STEP 2, I get an error:

$ git branch --set-upstream-to=origin/my_branch
error: the requested upstream branch 'origin/my_branch' does not exist
hint:
hint: If you are planning on basing your work on an upstream
hint: branch that already exists at the remote, you may need to
hint: run "git fetch" to retrieve it.
hint:
hint: If you are planning to push out a new local branch that
hint: will track its remote counterpart, you may want to use
hint: "git push -u" to set the upstream config as you push.

I know I can edit .git/config manually, but WHY doesn't the above work?


$ git remote -v
origin  XX:YYY/my_repo (fetch)
origin  XX:YYY/my_repo (push)

$ git --version
git version 2.9.2

解决方案

This bit is suspicious as the middle line should not occur:

$ git checkout -b my_branch
Branch my_branch set up to track local branch master.
Switched to a new branch 'my_branch'

For instance, when I do git checkout -b foo in a repository:

$ git checkout -b foo
Switched to a new branch 'foo'

Note the lack of an upstream setting. My guess is that your gco alias-or-script has --track inside it somewhere. Edit: This turns out to be because you'd configured branch.autoSetupMerge to always, so new branches created from local branches track their local branch.

This is only half of the problem, but if this were not happening, the full problem would not occur anyway. Hence one fix is to delete that setting (the default seems to be what most people mostly want).


Addressing the other half of the problem:

$ git branch --set-upstream-to=origin/my_branch
error: the requested upstream branch 'origin/my_branch' does not exist

The problem here is literally just what Git is saying: that origin/my_branch does not exist. Well, it doesn't exist yet: you need to convince your Git to have an origin/my_branch. Git being Git, there are a lot of ways to do this, but probably one best way, which we'll get to in a moment.

$ git push -u origin my_branch
Counting objects: 8, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (8/8), 1.16 KiB | 0 bytes/s, done.
Total 8 (delta 7), reused 0 (delta 0)
To XX:YYY/my_repo
   62d390c..4e4baa1  my_branch -> master
Branch my_branch set up to track remote branch master from origin.

This is where things go wrong: since my_branch already has an upstream setting of master (not origin/master, just master), your Git asks the other Git to use the name master, as if you had run:

git push origin my_branch:master

As there are no other problems with this git push, their side does what your side asks—sets their master to match your my_branch—and then your side does what you asked with -u, which is to change the upstream setting from master to origin/master. That's not what you meant to ask of course.

(And: it seems a little bit evil for your Git to substitute in master when that's a local master, i.e., branch.my_branch.remote is just ., not origin. But assuming we can't change Git itself...)

I see three easy ways to fix this:

  1. Use an explicit remote-side name: git push -u origin my_branch:my_branch. This overrides the current upstream setting, so that your Git asks their Git to write to my_branch. If that succeeds—it should—your Git will now have origin/my_branch and will change your upstream setting for my_branch to origin/my_branch.

    The one drawback here is that if the push fails, your Git won't change the current upstream setting. Of course this is true in general for git push -u (including both of the next two methods, but in those two cases there's no trap left behind for the future).

  2. Explicitly delete the current upstream setting before running the git push: git branch --unset-upstream my_branch (or anything equivalent, including editing the .git/config file). Now that there's no existing upstream, your Git won't ask their Git to use the name master.

    As soon as your Git asks their Git to create my_branch (rather than master) on origin, we're back to what happens with the first method.

  3. Avoid setting your local master as the upstream in the first place. The effect is the same as method 2.

One hard way to do this is to fake out your Git: you can create a remote-tracking branch without actually going to the remote. To do this you will have to use the "plumbing" command git update-ref, rather than git branch. If you do, though, you can then use --set-upstream-to before the push that actually creates the branch on the remote. (The other hard way is the trick you used of editing .git/config directly, or doing the equivalent with git config. This allows you to set the upstream to something that doesn't really exist yet, since all Git is really doing here is storing names.)

这篇关于分公司不断追踪主人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆