git 什么是跟踪分支?

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

What is a tracking branch?

gitversion-controlbranch

提问by jerhinesmith

Can someone explain a "tracking branch" as it applies to git?

有人可以解释适用于 git 的“跟踪分支”吗?

Here's the definition from git-scm.com:

这是来自git-scm.com的定义:

A 'tracking branch' in Git is a local branch that is connected to a remote branch. When you push and pull on that branch, it automatically pushes and pulls to the remote branch that it is connected with.

Use this if you always pull from the same upstream branch into the new branch, and if you don't want to use "git pull" explicitly.

Git 中的“跟踪分支”是连接到远程分支的本地分支。当您推拉该分支时,它会自动推拉到与其连接的远程分支。

如果您总是从同一个上游分支拉入新分支,并且不想明确使用“git pull”,请使用此选项。

Unfortunately, being new to git and coming from SVN, that definition makes absolutely no sense to me.

不幸的是,作为 git 新手并且来自 SVN,这个定义对我来说完全没有意义。

I'm reading through "The Pragmatic Guide to Git" (great book, by the way), and they seem to suggest that tracking branches are a good thing and that after creating your first remote (origin, in this case), you should set up your master branch to be a tracking branch, but it unfortunately doesn't cover why a tracking branch is a good thingor what benefits you get by setting up your master branch to be a tracking branch of your origin repository.

我正在阅读“ The Pragmatic Guide to Git”(顺便说一句,很棒的书),他们似乎认为跟踪分支是一件好事,并且在创建第一个远程(在这种情况下为原点)之后,您应该将您的主分支设置为跟踪分支,但不幸的是,它没有涵盖为什么跟踪分支是一件好事,或者通过将主分支设置为源存储库的跟踪分支可以获得什么好处

Can someone please enlighten me (in English)?

有人可以启发我(英文)吗?

采纳答案by Assaf Lavie

The ProGit bookhas a very good explanation:

ProGit本书一个很好的解释

Tracking Branches

跟踪分支

Checking out a local branch from a remote branch automatically creates what is called a tracking branch. Tracking branches are local branches that have a direct relationship to a remote branch. If you're on a tracking branch and type git push, Git automatically knows which server and branch to push to. Also, running git pullwhile on one of these branches fetches all the remote references and then automatically merges in the corresponding remote branch.

从远程分支签出本地分支会自动创建所谓的跟踪分支。跟踪分支是与远程分支有直接关系的本地分支。如果您在跟踪分支上并键入git push,Git 会自动知道要推送到哪个服务器和分支。此外,git pull在这些分支之一上运行时会获取所有远程引用,然后自动合并到相应的远程分支中。

When you clone a repository, it generally automatically creates a master branch that tracks origin/master. That's why git pushand git pullwork out of the box with no other arguments. However, you can set up other tracking branches if you wish — ones that don't track branches on origin and don't track the master branch. The simple case is the example you just saw, running git checkout -b [branch] [remotename]/[branch]. If you have Git version 1.6.2 or later, you can also use the --trackshorthand:

当你克隆一个仓库时,它通常会自动创建一个跟踪 origin/master 的 master 分支。这就是为什么git push,并git pull没有其他参数工作开箱。但是,如果您愿意,您可以设置其他跟踪分支——那些不跟踪原始分支和不跟踪主分支的分支。简单的例子就是你刚刚看到的例子,运行git checkout -b [branch] [remotename]/[branch]. 如果你有 Git 1.6.2 或更高版本,你也可以使用--track简写:

$ git checkout --track origin/serverfix
Branch serverfix set up to track remote branch refs/remotes/origin/serverfix.
Switched to a new branch "serverfix"

To set up a local branch with a different name than the remote branch, you can easily use the first version with a different local branch name:

要使用与远程分支不同的名称设置本地分支,您可以轻松地使用具有不同本地分支名称的第一个版本:

$ git checkout -b sf origin/serverfix
Branch sf set up to track remote branch refs/remotes/origin/serverfix.
Switched to a new branch "sf"

Now, your local branch sfwill automatically push to and pull from origin/serverfix.

现在,您的本地分支sf将自动推入和拉出origin/serverfix.

BONUS: extra git statusinfo

奖励:额外git status信息

With a tracking branch, git statuswill tell you whether how far behind your tracking branch you are - useful to remind you that you haven't pushed your changes yet! It looks like this:

使用跟踪分支,git status将告诉您是否落后于跟踪分支多远 - 有助于提醒您尚未推送更改!它看起来像这样:

$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

or

或者

$ git status
On branch dev
Your branch and 'origin/dev' have diverged,
and have 3 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

回答by hagrawal

Below are my personal learning notes on GIT tracking branches, hopefully it will be helpful for future visitors:

以下是我个人关于 GIT 跟踪分支的学习笔记,希望对以后的访问者有所帮助:

enter image description hereenter image description hereenter image description hereenter image description hereenter image description hereenter image description here

在此处输入图片说明在此处输入图片说明在此处输入图片说明在此处输入图片说明在此处输入图片说明在此处输入图片说明



Tracking branches and "git fetch":

跟踪分支和“git fetch”:

enter image description hereenter image description hereenter image description here

在此处输入图片说明在此处输入图片说明在此处输入图片说明

回答by VonC

The Pro Git bookmentions:

Pro Git 书提到

Tracking branches are local branches that have a direct relationship to a remote branch

跟踪分支是与远程分支有直接关系的本地分支

Not exactly. The SO question "Having a hard time understanding git-fetch" includes:

不完全是。SO 问题“很难理解git-fetch”包括:

There's no such concept of local trackingbranches, only remote trackingbranches.
So origin/masteris a remote tracking branch for masterin the originrepo.

没有本地跟踪分支的概念,只有远程跟踪分支。
所以origin/master是一个远程跟踪分行masterorigin回购。

But actually, once you establish an upstream branch relationshipbetween:

但实际上,一旦您在以下之间建立了上游分支关系

  • a local branch like master
  • and a remote tracking branch like origin/master
  • 一个当地的分支机构,如 master
  • 和一个远程跟踪分支,如 origin/master

Then you can consider masteras a local tracking branch: It tracks the remote tracking branchorigin/masterwhich, in turn, tracks the master branch of the upstream repoorigin.

然后您可以将其master视为本地跟踪分支:它跟踪远程跟踪分支origin/master,该分支又跟踪上游 repo的主分支origin

alt text

替代文字

回答by agrublev

This was how I added a tracking branch so I can pull from it into my new branch:

这就是我添加跟踪分支的方式,以便我可以从它拉入我的新分支:

git branch --set-upstream-to origin/Development new-branch