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

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

问题描述

有人可以解释一个跟踪分支,因为它适用于混帐?



以下是 git-scm.com


Git中的追踪分支是连接到远程
分支的本地
分支。当你推拉这个
分支时,它会自动推送,而
则拉到远程分支,它与
连接。



如果您总是从
相同的上游分支拉入新的
分支,并且您不希望明确使用
git pull,请使用此命令。


不幸的是,作为git和SVN的新手,这个定义对我来说毫无意义。



我正在阅读 Git的实用指南 (顺便提一下,这本书很棒),他们似乎建议跟踪分支是一件好事,在创建第一个远程(本例中为原点)之后,应该将主分支设置为跟踪分支,但不幸的是,它不包括为什么跟踪分支是一件好事或通过将主分支设置为跟踪来获得什么好处你可以给我一些启发(英文版)吗?

解决方案

ProGit book 一个非常好的解释

跟踪分支



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



当您克隆存储库时,它通常会自动创建一个跟踪原点/主点的主分支。这就是为什么git push和git pull在没有其他参数的情况下开箱即可使用的原因。但是,如果您愿意,您可以设置其他跟踪分支 - 不跟踪原始分支并且不跟踪主分支的分支。简单的例子就是刚刚看到的例子,运行 git checkout -b [branch] [remotename] / [branch] 。如果您的Git版本为1.6.2或更高版本,您还可以使用 - track 简写:

  $ git checkout --track origin / serverfix 
设置分支serverfix以跟踪远程分支refs / remotes / origin / serverfix。
切换到一个新的分支serverfix

设置一个不同的本地分支名称比远程分支,你可以很容易地使用第一个版本与不同的本地分支名称:

  $ git checkout -b sf origin / serverfix 
分支sf设置为跟踪远程分支refs / remotes / origin / serverfix。
切换到新分支sf

现在,您的本地分支 sf 会自动推送到 origin / serverfix


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

Here's the definition from 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.

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

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

解决方案

The ProGit book has a very good explanation:

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

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"

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

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

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