是我的分支跟踪掌握? [英] is my branch tracking to master?

查看:68
本文介绍了是我的分支跟踪掌握?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做到了这一点:

  git branch --track stats_page 

创建一个单独的分支来尝试一些东西。但现在每次我推或拉我看:

  $ git pull 
From。
*分支主人 - > FETCH_HEAD

  $ git push 
共0(delta 0),重用0(delta 0)
到。
ff0caf5..e360168 stats_page - > master

我远离git master,所以我可能会错过理解发生的事情 - 但是我的stats_page分支跟踪到master?



我可能已经创建了分支错误 - 如果是这样 - 我可以修复它,以便从现在开始推送到远程stats_page分支?如何解决这个问题最简单的方法是在 / questions / 520650 / how-do-you-make-an-existing-git-branch-track-a-remote-branch>让一个现有的Git分支跟踪一个远程分支?



这里发生了什么



我总是发现git的跟踪分支的描述让我感到困惑。他们听起来,至少对我来说,就像他们做的比他们真正做的多得多。



我认为,诀窍是要意识到git并没有真正实现跟踪远程存储库。 (它会从远程仓库中 fetch )。相反,追踪分支追踪您自己的仓库中的分支 。要跟踪远程分支,您创建了一个分支,其名称说明它最初是从其他某个分仓中复制的。 (在封面下,这意味着任何分支存储在 refs / remotes / 中,或多或少)。最常见的其他回购是名为 origin ,这是初始 git clone 为您设置的名称。正如它在git-clone手册页中所说:


这个默认配置是通过在<$ c下创建对远程分支头的引用来实现的$ c> refs / remotes / origin 并通过初始化 remote.origin.url remote.origin.fetch code>配置变量。

当您使用 git branch --track 创建分支,它使新分支跟踪您提供的起点。 (而且,跟踪主要意味着关联各种推/拉/取动作,并且当我运行 git status 时告诉我更多。)因此,假设您克隆的回购-from已经有一个 stats_page 分支,你可能需要这样做:

  git branch --track stats_page origin / stats_page 

告诉git 创建一个名为 stats_page 的新分支,它跟踪我在 refs / remotes / origin / stats_page 中的内容(您通常会离开关闭 refs / heads refs / remotes 部分,git会计算出来),甚至不需要 - track 在这里,因为右边的 origin / 部分为你打开。



一旦你这么做了,用git的术语来说,你有一个本地分支( refs / heads / stats_page ),一个远程分支( refs / remotes / origin / stats_page )。要实际更新远程分支,请使用 git f ,或者更明确地说, git fetch origin ,它出现在与 origin

(我已经调用了gitSCM的Borg :我们是git-Borg,我们会将自己的独特性加入到我们自己的行列中。你所做的大部分工作只是将新东西添加到自己的repo-Borg集体中。)



使用 git fetch 来更新远程分支的本地副本( origin / stats_page > git merge 来将它合并到( stats_page 中,名字没有 origin / 部分)。而且,一个 git pull 只需一个简单的步骤就可以执行这两个操作 - 这隐藏了事实上它实际上是 fetch 从远程获取新东西



现在,您遇到的问题是,当您运行时:

  git branch --track stats_page 

您在<$ code> master 分支,因此您创建了一个名为 stats_page 的新本地分支,用于跟踪您的 master ,而不是跟踪 origin / stats_page 。因此,只需在名为 master的本地分支上拖动并按 c ,然后 git push / code>。 (注意:如果你真的运行 git fetch ,它仍然会读入 remotes / origin / * ,所以它仍然会更新 origin / stats_page ,尽管一个本地分支错误地跟踪了另一个本地分支。)

最后一个注意事项:通常,当您创建一个追踪同名远程分支的本地分支时,您还需要立即执行 git checkout 。您可以只需 git checkout -b stats_page origin / stats_page 即可同时完成所有三个步骤。 (如果你的git版本比较旧,当你在右侧命名 origin / stats_page 时,它可能不会自动跟踪;但如果是这样的话,你可能应该更新你的git版本。 )



脚注:有人建议不要使用 git pull ,我有点赞同它们。 git pull 确实混合了两种非常不同的操作。特别是 git pull remote_A remote_B 似乎让人们在章鱼合并时感到惊讶。使用 git fetch 绝不会让人感到意外;一旦他们习惯并且熟悉一个明确的 git merge ,快速合并背后的逻辑就更有意义了。不过,没有参数的 git pull 非常方便....


I did this:

git branch --track stats_page

to create a separate branch to try something out. But now everytime I push or pull I see:

$ git pull
From .
 * branch            master     -> FETCH_HEAD

and

$ git push
Total 0 (delta 0), reused 0 (delta 0)
To .
   ff0caf5..e360168  stats_page -> master

I'm far from a git master so I might be miss understanding what's happening - but is my stats_page branch tracking to master?

I may have created the branch wrong - if so - can I fix it so that from now on it pushes and pulls to the remote stats_page branch? How?

解决方案

Quick answer: The easiest way to fix this is in Make an existing Git branch track a remote branch?

What's really going on here

I've always found descriptions of git's "tracking branches" confusing myself. They sound, to me at least, like they do a lot more than they really do.

The trick, I think, is to realize that git doesn't ever actually "track" a remote repository at all. (It does fetch from a remote repo.) Instead, a "tracking branch" tracks a branch in your own repo. To "track a remote branch", you create a branch whose name says that it was originally copied from some other repo. (Under the covers, this means any branch stored under refs/remotes/, more or less.) The most common "other repo" is the one named origin, which is the one that an initial git clone sets up for you. As it says in the git-clone manual page:

This default configuration is achieved by creating references to the remote branch heads under refs/remotes/origin and by initializing remote.origin.url and remote.origin.fetch configuration variables.

When you use git branch --track to create a branch, it makes the new branch "track" the "start point" that you give it. (And, "track" mainly means "associate for various push/pull/fetch actions, and tell me more when I run git status.) So, assuming the repo you cloned-from already has a stats_page branch, you probably meant to do this:

git branch --track stats_page origin/stats_page

which tells git "make new branch named stats_page that tracks what I have in refs/remotes/origin/stats_page" (you normally leave off the refs/heads and refs/remotes parts, and git figures them out). You don't even need the --track here, as the origin/ part on the right turns that on for you.

Once you do all that, in git's terminology, you have a "local" branch (refs/heads/stats_page) that tracks a "remote" branch (refs/remotes/origin/stats_page). To actually update the "remote" branch you use git fetch, or more explicitly, git fetch origin, which goes out to the URL associated with origin and collects any new stuff and then adds it to your repo. Hence, despite calling this "remote", it's still remarkably local: it's right there on your own local disk.

(I have taken to calling git "the Borg of SCMs": "We are the git-Borg. We will add your commit distinctiveness to our own." Most of what you do just adds new stuff to your own repo-Borg-collective.)

Once you've used git fetch to update your local copy of the "remote" branch (origin/stats_page), you can do a git merge to get it merged in (to stats_page, the name without the origin/ part). And, a git pull just runs those two in one easy step—which hides the fact that it's the fetch that actually gets new stuff from the "remote"

Now, the problem you have is that when you ran:

git branch --track stats_page

you were on your master branch, so you created a new "local" branch named stats_page that tracks your master, instead of tracking origin/stats_page. Hence, git pull and git push just pull and push on your local branch named master. (Note: If you actually run git fetch, that still fetches into remotes/origin/*, so it still updates origin/stats_page, even though the one "local" branch is mistakenly tracking another "local" branch.)

One last note: usually when you make a "local" branch that tracks a remote branch of the same name, you also want to do a git checkout immediately. You can just git checkout -b stats_page origin/stats_page to get all three steps done at once. (If your version of git is old, it may not automatically track when you name origin/stats_page on the right; but if so you probably should just update your version of git.)


Footnote: Some people recommend against using git pull at all, and I sort of agree with them. git pull really mixes two very different operations. In particular git pull remote_A remote_B seems to surprise people when it does an octopus merge. Using git fetch never seems to surprise people; and once they get used to, and familiar with, an explicit git merge, the logic behind fast-forward merges makes a lot more sense. Still, git pull with no arguments is so convenient....

这篇关于是我的分支跟踪掌握?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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