什么是跟踪分支? [英] What is a tracking branch?

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

问题描述

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

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

这是 git-scm.com


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

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.

如果始终从同一上游分支
进入新的
分支,并且不想显式使用
git pull,请使用此选项。

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来说是新手,来自SVN,所以这个定义对我来说绝对没有意义。

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

我正在阅读 《实用Git指南》 (顺便说一句,这本好书),它们似乎暗示了跟踪分支是一件好事,并且在创建了第一个远程对象(在本例中为起源)之后,您应该将主分支设置为跟踪分支,但不幸的是,它没有涵盖为什么跟踪分支是一件好事将主分支设置为跟踪分支会带来什么好处

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 thing or what benefits you get by setting up your master branch to be a tracking branch of your origin repository.

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

Can someone please enlighten me (in English)?

推荐答案

ProGit图书具有一个很好的解释

跟踪分支

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

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 pull while on one of these branches fetches all the remote references and then automatically merges in the corresponding remote branch.

克隆存储库时,它通常会自动创建一个跟踪源/主数据库的主数据库分支。这就是为什么 git push git pull 开箱即用而没有其他参数的原因。但是,您可以根据需要设置其他跟踪分支,即不跟踪原点分支也不跟踪主分支的分支。最简单的例子是您刚才看到的示例,运行 git checkout -b [branch] [remotename] / [branch] 。如果您具有Git 1.6.2或更高版本,则还可以使用-track 的简写:

When you clone a repository, it generally automatically creates a master branch that tracks origin/master. That’s why git push and git pull work 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 --track shorthand:

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

现在,您当地的分支机构 sf 会自动从来源/服务器修补程序推入和拉出。

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

奖金:额外的 git状态信息

BONUS: extra git status info

带有跟踪分支的 git status 会告诉您您是否离跟踪分支多远-有助于提醒您尚未推送更改!看起来像这样:

With a tracking branch, git status will 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
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

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

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

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