git中的'跟踪'概念有不同的含义吗? [英] Are there different meanings to the concept of 'tracking' in git?

查看:182
本文介绍了git中的'跟踪'概念有不同的含义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行'git branch -r'并获得


origin / branch1



origin / branch2

在手册页中,-r选项将列出或删除(如果与-d一起使用)远程追踪分支。所以origin / branch1和origin / branch2被称为远程跟踪分支。但是,您不能直接提交到远程跟踪分支(将改为创建匿名分支)。远程追踪分支在运行'git fetch'时只需跟踪远程分支。



以下是语义对我来说有点模糊的地方。如果我然后


git checkout -b branch1 origin / branch1


我得到以下输出:
分支分支1设置为从原点跟踪远程分支branch1。切换到新分支'branch1'



以下是我的问题,尽可能详细地说明什么让我感到困惑...
由于将branch1设置为从原点跟踪远程分支branch1,因此'branch1'因此被认为是远程跟踪分支?如果是这样,这与'origin / branch1'在运行'git branch -r'时已被列为远程跟踪分支的事实不矛盾吗?据我所知,存在本地(主题)分支或远程跟踪分支。当运行'git checkout -b branch1 origin / branch1'时,我是否创建了一个本地(主题)分支(我可以在其上添加提交),通过提取方式跟踪远程分支?运行'git分支'现在给出:'* branch1',并且运行'git branch -r'仍然给出'origin / branch1'和'origin / branch2'。我创建了branch1来添加提交到并跟踪origin / branch1。哪一个被认为是远程跟踪分支,'git branch'的输出中的'branch1',或'git branch -r'的输出中的'origin / branch1'?

解决方案

这是一个关于git术语特别讨厌的好问题,尽管这个项目似乎正在慢慢解决。



基本上,track意味着表达式(a)远程跟踪分支和(b)设置为跟踪远程分支的 branch1 code> branch1 from origin 。以下是一个简要的摘要: 远程追踪分支:远程追踪分支通常是分支更新由 git fetch ,因此, git pull .¹您可以将它们视为缓存远程存储库中分支的状态。你可以从他们合并,检查他们的历史等,但你不能直接在他们身上工作。这个短语中的跟踪意味着远程跟踪分支表示远程跟踪分支上次更新时远程存储库中分支的状态。 分支 foo 设置为从 origin 跟踪远程分支:在这句话中,你被告知git已经设置将本地分支 foo 与远程跟踪分支 origin / bar 相关联的配置变量。这样可以实现很好的功能,例如在分支 foo 时可以输入 git pull ,以便获取和然后从 origin / bar 合并。这也是你如何得到有用的信息关于你的分支相对于远程跟踪分支的状态,比如你的分支 foo 是24前提交 origin / bar 可以被快速转发。您被告知您的本地分支正在跟踪 远程跟踪分支相关联。您还可以听到 origin / bar 相对于 foo 上游

因此,这些跟踪/追踪的感觉是完全不同的,可悲的是它是一个常见的混乱来源。
$ b

然而,第二种意义似乎正在慢慢被弃用,例如, push.default 的其中一个可能选项是 tracking ,但现在弃用这个选项来支持选项名称上游






因此,直接回答您的问题:


凭借branch1设置为从原点跟踪远程分支branch1,是'branch1',因此被认为是远程跟踪分支?

否, branch1 不是远程跟踪分支。


运行'git checkout -b branch1 origin / branch1',我创建一个本地(主题)分支(到)我可以添加提交),通过提取方式跟踪远程分支?


呃,有点 - 跟踪(感觉2 )远程跟踪分支,后者通过提取从远程存储库中的分支进行更新。 (就个人而言,我试图避免使用远程分支这个术语,以支持远程仓库中的分支,以防万一人们认为您的意思是远程跟踪分支。)


运行'git branch'现在给出:'* branch1',并且运行'git branch -r'仍然给出'origin / branch1'和'origin / branch2'。我创建了branch1来添加提交到并跟踪origin / branch1。哪一个被认为是远程跟踪分支,'git branch'输出中的'branch1',或'git branch -r'输出中的'origin / branch1'?

远程追踪分支是 origin / branch1






¹当您成功完成 git push 到远程存储库中的相应分支时,它们也会更新。


I run 'git branch -r' and get

origin/branch1

origin/branch2

From the man page, the -r option will "list or delete (if used with -d) the remote-tracking branches". So origin/branch1 and origin/branch2 are known as remote-tracking branches. However, you can't commit directly onto a remote-tracking branch (an anonymous branch will be created instead). A remote-tracking branch simply tracks a remote branch when running 'git fetch'.

Here's where the semantics get a little blurry for me. If I then

git checkout -b branch1 origin/branch1

I get the following output: "Branch branch1 set up to track remote branch branch1 from origin. Switched to a new branch 'branch1'"

Here's my question, put as verbosely as possible to clarify what's confusing me... By virtue of having branch1 set up to track remote branch branch1 from origin, is 'branch1' thus considered a remote-tracking branch? If so, doesn't this conflict with the fact that 'origin/branch1' was already listed as a remote tracking branch when running 'git branch -r'? From what I understand, there exist either local (topic) branches or remote-tracking branches. When running 'git checkout -b branch1 origin/branch1', am I creating a local (topic) branch (onto which I can add commits) that is tracking a remote branch by way of fetches? Running 'git branch' now gives: '* branch1', and running 'git branch -r' still gives 'origin/branch1' and 'origin/branch2'. I created branch1 to add commits to and to track origin/branch1. Which is considered the remote-tracking branch, 'branch1' from the output of 'git branch', or 'origin/branch1' from the output of 'git branch -r'?

解决方案

This is a good question about a particularly annoying bit of git terminology, albeit one that the project seems to be slowly fixing.

Basically, "track" means something very different in the expressions (a) "remote-tracking branch" and (b) "branch1 set up to track remote branch branch1 from origin". Here's a quick summary:

  1. "remote-tracking branch": remote-tracking branches are branches that are usually updated by git fetch, and, consequently, git pull.¹ You can think of these as like a cache of the state of the branch in the remote repository. You can merge from them, examine their history, etc. but you can't work directly on them. "Track" in this phrase means that the remote-tracking branch represents the state of the branch in the remote repository the last time that remote-tracking branch was updated.
  2. Branch foo set up to track remote branch bar from origin: in this phrase what you're being told is that git has set up configuration variables that associate your local branch foo with the remote-tracking branch origin/bar. This enables nice features like being able to just type git pull while you're on branch foo in order to fetch and then merge from origin/bar. It's also how you get helpful the messages about the state of your branch relative to the remote-tracking branch, like "Your branch foo is 24 commits ahead of origin/bar can can be fast-forwarded". You're being told that your local branch is tracking has been associated with a remote-tracking branch. You also hear this referred to as origin/bar being upstream with respect to foo.

So, these senses of track / tracking are quite different, and sadly it's a common source of confusion.

The second sense seems to be being slowly deprecated, however - for example, one of the possible options for push.default used to be tracking, but this is now deprecated in favour of the option name upstream.


So, to answer your questions directly:

By virtue of having branch1 set up to track remote branch branch1 from origin, is 'branch1' thus considered a remote-tracking branch?

No, branch1 is not a remote-tracking branch.

When running 'git checkout -b branch1 origin/branch1', am I creating a local (topic) branch (onto which I can add commits) that is tracking a remote branch by way of fetches?

Well, sort of - it's tracking (sense 2) a remote-tracking branch, and the latter is updated from a branch in the remote repository by fetches. (Personally, I try to avoid the term "remote branch", in favour of "the branch in the remote repository", just in case people think you mean a remote-tracking branch.)

Running 'git branch' now gives: '* branch1', and running 'git branch -r' still gives 'origin/branch1' and 'origin/branch2'. I created branch1 to add commits to and to track origin/branch1. Which is considered the remote-tracking branch, 'branch1' from the output of 'git branch', or 'origin/branch1' from the output of 'git branch -r'?

The remote-tracking branch is origin/branch1.


¹ They're also updated when you make a successful git push to the corresponding branch in the remote repository.

这篇关于git中的'跟踪'概念有不同的含义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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