有人能解释一下应用于git的“跟踪分支”吗?
以下是来自git-scm.com的定义:
Git中的“跟踪分支”是一个本地分支
与远程连接的分支
分支。当你用力拉它的时候
分支,它自动推和
拉到它所在的远端分支
与。
如果你总是从
相同的上游分支进入新的
布兰奇,如果你不想用
"git pull"。
不幸的是,作为git的新手和来自SVN的新手,这个定义对我来说完全没有意义。
我阅读“Git实用指南”(伟大的书,顺便),和他们似乎表明,跟踪分支是一件好事,在创建你的第一个远程(起源,在这种情况下),你应该设置主分支跟踪分支,但不幸的是不包括为什么跟踪分支是一件好事或得到什么好处通过设置你的主分支是一个跟踪分支起源库。
有人能(用英语)给我开导一下吗?
Pro Go书中提到:
跟踪分支是与远程分支有直接关系的本地分支
不完全是。SO问题“很难理解git-fetch”包括:
没有本地跟踪分支的概念,只有远程跟踪分支。
因此,origin/master是在origin repo中对master的远程跟踪分支。
注意,Git 2.37 (Q3 2022)允许你自动设置远程跟踪分支,包括:
git config --global push.autoSetupRemote true
但实际上,一旦你建立了上游分支关系:
一个地方分支像师父
还有一个远程跟踪分支,比如origin/master
你可以用git branch -avv列出它们。
然后,您可以将master视为一个本地跟踪分支:它跟踪远程跟踪分支原点/master,而master又跟踪上游回购原点的主分支。
换句话说:“远程跟踪分支”中的“远程”意味着远程分支的本地副本(跟踪),以记住最后获取的内容。
Michael Freidgeim在评论中补充道:
术语“远程跟踪分支”令人困惑。
更准确的说法是:
“位于本地的远程跟踪分支”。
否则,有人可能会解释为“远程跟踪分支”位于源服务器上。
ProGit的书有一个很好的解释:
跟踪分支
从远程分支签出本地分支会自动创建所谓的跟踪分支。跟踪分支是与远程分支有直接关系的本地分支。如果你在跟踪分支上,输入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 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"
要设置一个与远程分支名称不同的本地分支,您可以轻松地使用第一个版本的不同本地分支名称:
$ 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将自动推入和拉入origin/serverfix。
奖励:额外的git状态信息
有了跟踪分支,git状态会告诉你你落后于跟踪分支多远——这有助于提醒你还没有推送你的更改!它是这样的:
$ 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)