在 Git 中,本地分支可以相互跟踪 - 这有什么用?
我听说在 Git
中,您可以让本地分支 A
跟踪另一个本地分支 B
。
为什么有人想要这样做?
I heard that in Git
, you can let a local branch A
track another local branch B
.
Why would someone want to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
让本地分支跟踪另一个本地分支时想到的主要事情是(1)来自 Git 的关于某个分支在被跟踪分支之前/之后的更多通知消息以及(2)触发钩子。
Git 显示更多信息的一个区域是创建分支时。创建 基本分支 如下所示:
创建 跟踪分支 如下所示:
这将在
.git/config
中添加以下内容:在分支上提交一些更改后
A
和B
,在分支A
上执行git status -s -b
在分支上时显示
它显示## A
B## B...master [ahead 1, Behind 1]
,提供一些有关分支B
和之间关系的快速信息大师。
您可能希望本地分支跟踪另一个本地分支的另一个区域是触发 hooks;特别是
git 推送
。例如,您可能有一些钩子来触发持续集成服务器上的构建、执行一些许可证头检查、检查空白格式错误等。The main things that come to mind for having a local branch track another local branch are (1) more informed messages from Git regarding a branch being ahead/behind of the tracked branch and (2) trigger hooks.
One area Git displays more information is when creating a branch. Creating a basic branch looks like the following:
While creating a tracking branch looks like:
This would add the following in
.git/config
:After committing some changes on branches
A
andB
, executinggit status -s -b
on branchA
displays## A
while on branchB
it displays## B...master [ahead 1, behind 1]
, providing some quick information regarding the relationship between branchesB
andmaster
.The other area where you might want a local branch track another local branch is to trigger hooks; in particular
pre-receive
,update
,post-receive
andpost-update
during agit push
. You might have hooks to, for example, trigger a build on a continuous integration server, do some license header checks, check for white space format errors, etc.在很多情况下,跟踪另一个本地分支是有用的。例如,在某些 git 工作流程中,有一些东西可以保护 master 不直接接收推送请求。一个例子是代码审查或持续集成系统,它必须在提交登陆到远程分支之前通过。另一个例子是,当一个项目由一组仅接受拉取请求的提交者管理时(GitHub 项目经常这样做)。作为开发人员,我可能想要创建一个功能分支,然后提交该分支以供审核(或向存储库提交者提交拉取请求)。然后,我可能想继续本地开发,而我的队友异步审查我的代码并完成 CI 构建。为了在之前的提交之上继续开发,我可以创建第二个本地分支来跟踪我的第一个本地分支。这允许我从第一次提交开始构建,即使该提交尚未到达上游远程分支。另外,如果有人建议对我的第一个分支进行代码审查更改,或者 CI 构建失败,我可以更新该分支,然后将这些更改重新设置为下游本地分支。以下是如何设置一个分支来跟踪另一个本地分支。
给定本地功能分支:
这表明本地跟踪分支
master
和branch-1
都跟踪origin< 上的
master
分支/代码> 远程。我现在可以创建另一个分支,并将其设置为跟踪本地跟踪分支branch-1
。There are many occasions when tracking another local branch is useful. For example, in some git workflows, there something in place protecting master from directly receiving push requests. One example is a code review or continuous integration system, which must pass prior to a commit landing on the remote branch. Another example is when a project is managed by a set of committers that only accept pull requests (GitHub projects often do this). As a developer, I may want to create a feature branch, and then submit that branch for review (or submit a pull request to a repo committer). I then may want to continue local development while my teammates asynchronously review my code and the CI builds complete. To continue development on top of my previous commit, I may create a second local branch that tracks from my first local branch. This allows me to build from my first commit, even though that commit hasn't made its way to the upstream remote branch. Also, if someone suggests code review changes for my first branch, or the CI build fails, I can update that branch, and then rebase those changes into down-stream local branches. Here is how to set up a branch to track another local branch.
Given a local feature branch:
This shows that local tracking branches
master
andbranch-1
both track themaster
branch on theorigin
remote. I can now create another branch, and set it up to track local tracking branchbranch-1
.请注意,只有在branch.B.merge 配置为严格定义:
refs/heads/master
。如果松散定义它就不起作用:“
master
”。但是通过 commit 05e7368,由 Junio C Hamano (
gitster
)对于 Git 2.3.0(2015 年第一季度),这也适用。Note that the ahead/behind information that you have between one branch '
B
' and another 'A
' tracked by the first only works if the branch.B.merge config is strictly defined:refs/heads/master
.It wouldn't work if it is loosely define: '
master
'.But with commit 05e7368, done by Junio C Hamano (
gitster
) for Git 2.3.0 (Q1 2015), this will work too.我能想到的一个例子是,如果您有一个“稳定”分支。那么如果您可以创建一个新分支(例如“实验”)并让它跟踪稳定分支,那就太好了。
除此之外,它可能是为了一致性(这只是一个猜测)。
One example I can think of is if you have a 'stable' branch. Then it would be nice if you could make a new branch, 'experiment' for example, and let it track the stable branch.
Other than that it might be for consistency (that's just a guess).