本地分支跟踪其他本地分支的用例是什么?

18
在 Git 中我们可以这样做:

$ git checkout -b testbranch --track master
Branch testbranch set up to track local branch master.
Switched to a new branch 'testbranch'
这些分支的用例是什么?

“仅仅因为你能做到,并不意味着你应该这么做。” :) 我不知道答案。 - Sergio Tulentsev
@SergioTulentsev,实际上你没有必要不这样做。(它并不会本质上破坏任何东西。) 这更像是一个“为什么?--因为我们可以。”的事情,可能是在 Git 的跟踪实现中自动包含的。这可能只是很难想出任何有意义的用例 :D - Nevik Rehnel
@NevikRehnel:这正是我想说的 :) - Sergio Tulentsev
请注意,这几乎是 https://dev59.com/o2oy5IYBdhLWcg3wnfUi 的重复。虽然为了保留这里的答案和高投票率,我们可能想去标记那个问题为这个问题的重复?@SergioTulentsev? - Nevik Rehnel
正如我们所指出的,这并不是Git的常用功能;而且你的问题似乎并不是很紧迫(在这种情况下,它更像是一个“我该怎么做?”的问题,而不是“我可以用这个做什么?”哈哈)。 - Nevik Rehnel
显示剩余2条评论
2个回答

6

将一个本地分支设置为跟踪另一个本地分支通常并不实用,但我可以想到一些情况下它可能是有用的:

  • You may have some aliases or scripts that do stuff with upstream branches (e.g., via the @{u} shorthand; see git help revisions). If so, configuring a local branch to track another local branch would allow you to test your aliases or scripts without affecting the remote repository or creating a temporary dummy repository.

  • Suppose you are working on a new local branch named foo off of your local master branch (which tracks origin/master) but you aren't ready to publish foo to origin yet. Or maybe you don't ever want to push to origin -- perhaps the commits added a bunch of obnoxious printf() calls you sprinkled in to help with debugging. Anyway, whenever master is updated, you want to rebase foo onto master. Normally you would just type git rebase master, but if you're like me you may find yourself frequently typing just git rebase out of habit. If you do, you'll get this error message:

    $ git rebase
    There is no tracking information for the current branch.
    Please specify which branch you want to rebase against.
    See git-rebase(1) for details
    
        git rebase <branch>
    
    If you wish to set tracking information for this branch you can do so with:
    
        git branch --set-upstream-to=origin/<branch> foo
    

    You may be tempted to do this:

    git branch --set-upstream-to=origin/master foo
    

    but that can have devastating effects if you're in the habit of typing git push every few commits (you'll end up pushing your work-in-progress commits on your local foo branch to origin's master branch, assuming you have push.default set to upstream like I do).

    Instead, you can configure the local foo branch to track your local master branch. Now your habit of typing git rebase does what you want, and an absent-minded git push will only mess up your local master branch, not origin's master branch.

    But it's usually not worthwhile to configure a local branch to track another local branch just for git rebase. I think it's easier to live with remembering to type git rebase master instead of git rebase. If I forget, I get an error message that jogs my memory and I try again. No big deal.


啊,终于有一个我可以自己点赞的答案了!谢谢,Richard。 - Eli Bendersky

0
我尝试了这个命令,并发现主分支将作为“真正的”远程分支执行。
在.git/config中,添加了以下内容:
[branch "testbranch"]
    remote = .
    merge = refs/heads/master

你可以使用$git pull从主分支拉取代码,使用

$git push . testbranch:master将代码推送到主分支。

或者你可以在.gitconfig中添加以下配置后,直接输入$git push即可:

[push]
    default = upstream

从一个分支推送到另一个分支在语义上意味着什么?如果我想将一个分支合并到主分支,我会使用 merge(或 rebase)。 - Eli Bendersky
也许在进行 git push 时,你可以触发 hooks 来进行一些检查或其他操作。:-P - pktangyue

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接