在 Git 中,本地分支可以相互跟踪 - 这有什么用?

发布于 2024-12-20 03:21:37 字数 100 浏览 0 评论 0原文

我听说在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

挽你眉间 2024-12-27 03:21:37

让本地分支跟踪另一个本地分支时想到的主要事情是(1)来自 Git 的关于某个分支在被跟踪分支之前/之后的更多通知消息以及(2)触发钩子。

Git 显示更多信息的一个区域是创建分支时。创建 基本分支 如下所示:

$ git co -b A master
Switched to a new branch 'A'

创建 跟踪分支 如下所示:

$ git co --track -b B master
Branch B set up to track local branch master.
Switched to a new branch 'B'

这将在 .git/config 中添加以下内容:

[branch "B"]
    remote = .
    merge = refs/heads/master

在分支上提交一些更改后 AB,在分支 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:

$ git co -b A master
Switched to a new branch 'A'

While creating a tracking branch looks like:

$ git co --track -b B master
Branch B set up to track local branch master.
Switched to a new branch 'B'

This would add the following in .git/config:

[branch "B"]
    remote = .
    merge = refs/heads/master

After committing some changes on branches A and B, executing git status -s -b on branch A displays ## A while on branch B it displays ## B...master [ahead 1, behind 1], providing some quick information regarding the relationship between branches B and master.

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 and post-update during a git 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.

瞄了个咪的 2024-12-27 03:21:37

在很多情况下,跟踪另一个本地分支是有用的。例如,在某些 git 工作流程中,有一些东西可以保护 master 不直接接收推送请求。一个例子是代码审查或持续集成系统,它必须在提交登陆到远程分支之前通过。另一个例子是,当一个项目由一组仅接受拉取请求的提交者管理时(GitHub 项目经常这样做)。作为开发人员,我可能想要创建一个功能分支,然后提交该分支以供审核(或向存储库提交者提交拉取请求)。然后,我可能想继续本地开发,而我的队友异步审查我的代码并完成 CI 构建。为了在之前的提交之上继续开发,我可以创建第二个本地分支来跟踪我的第一个本地分支。这允许我从第一次提交开始构建,即使该提交尚未到达上游远程分支。另外,如果有人建议对我的第一个分支进行代码审查更改,或者 CI 构建失败,我可以更新该分支,然后将这些更改重新设置为下游本地分支。以下是如何设置一个分支来跟踪另一个本地分支。

给定本地功能分支:

$ git co -b branch-1
$ git branch -u origin/master
Switched to a new branch 'branch-1'
$ git branch -vv
* branch-1                9f0c361 [origin/master] Some commit message
  master                  85ede1a [origin/master] Some commit message

这表明本地跟踪分支 masterbranch-1 都跟踪 origin< 上的 master 分支/代码> 远程。我现在可以创建另一个分支,并将其设置为跟踪本地跟踪分支 branch-1

$ git co -b branch-2
Switched to a new branch 'branch-2'
$ git branch -u branch-1 branch-2
Branch branch-2 set up to track local branch branch-1 by rebasing.
$ git branch -vv
  branch-1                85ede1a [origin/master] Some commit message
* branch-2                85ede1a [branch-1] Some commit message
  master                  85ede1a [origin/master] Some commit message

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:

$ git co -b branch-1
$ git branch -u origin/master
Switched to a new branch 'branch-1'
$ git branch -vv
* branch-1                9f0c361 [origin/master] Some commit message
  master                  85ede1a [origin/master] Some commit message

This shows that local tracking branches master and branch-1 both track the master branch on the origin remote. I can now create another branch, and set it up to track local tracking branch branch-1.

$ git co -b branch-2
Switched to a new branch 'branch-2'
$ git branch -u branch-1 branch-2
Branch branch-2 set up to track local branch branch-1 by rebasing.
$ git branch -vv
  branch-1                85ede1a [origin/master] Some commit message
* branch-2                85ede1a [branch-1] Some commit message
  master                  85ede1a [origin/master] Some commit message
∞梦里开花 2024-12-27 03:21:37

请注意,只有在branch.B.merge 配置为严格定义:refs/heads/master
如果松散定义它就不起作用:“master”。

但是通过 commit 05e7368,由 Junio C Hamano (gitster)对于 Git 2.3.0(2015 年第一季度),这也适用。

当检查一个设置为在另一个分支之上构建的分支时
分支(通常是远程跟踪分支),“git checkout”报告如何
您的工作与其他分支相关,例如

Your branch is behind 'origin/master', and can be fast-forwarded.

引入此功能时,这只适用于构建在远程跟踪分支上的分支,但是 5e6e2b4(使本地分支在以下情况下表现得像远程分支) --tracked, 2009-04-01, git 1.6.3) 添加了支持,为构建在其他本地分支上的分支(即 branch.*.remote< 的分支)提供相同的报告/code> 变量设置为“.”)。
然而,与对在远程跟踪分支上构建的分支的支持不同,这没有考虑到branch.*.merge配置允许记录缩短的分支名称的事实.

branch.*.merge 设置为“master”(而不是“refs/heads/master”)时,即“my分支构建在本地“master”分支上”,这导致“git checkout”报告:

Your branch is based on 'master', but the upstream is gone.

上游是我们的存储库,绝对不会消失,所以这个
输出是废话。

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.

When checking out a branch that is set to build on top of another
branch (often, a remote-tracking branch), "git checkout" reports how
your work relates to the other branch, e.g.

Your branch is behind 'origin/master', and can be fast-forwarded.

Back when this feature was introduced, this was only done for branches that build on remote-tracking branches, but 5e6e2b4 (Make local branches behave like remote branches when --tracked, 2009-04-01, git 1.6.3) added support to give the same report for branches that build on other local branches (i.e. branches whose branch.*.remote variables are set to '.').
Unlike the support for the branches building on remote-tracking branches, however, this did not take into account the fact that branch.*.merge configuration is allowed to record a shortened branch name.

When branch.*.merge is set to 'master' (not 'refs/heads/master'), i.e. "my branch builds on the local 'master' branch", this caused "git checkout" to report:

Your branch is based on 'master', but the upstream is gone.

The upstream is our repository and is definitely not gone, so this
output is nonsense.

香草可樂 2024-12-27 03:21:37

我能想到的一个例子是,如果您有一个“稳定”分支。那么如果您可以创建一个新分支(例如“实验”)并让它跟踪稳定分支,那就太好了。

git checkout --track -b experiment stable
* do some experiments with some commits *
git push

除此之外,它可能是为了一致性(这只是一个猜测)。

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.

git checkout --track -b experiment stable
* do some experiments with some commits *
git push

Other than that it might be for consistency (that's just a guess).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文