如何使用git设置分支的多个跟踪级别 [英] How to setup multiple tracking levels of branchs with git

查看:191
本文介绍了如何使用git设置分支的多个跟踪级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个git本地分支,该分支跟踪了远程svn中继.

I have a git local branch that tracked the remote svn trunk.

我创建了一个新的开发分支,并设置了本地分支来对其进行跟踪.

I created a new development branch and set the local branch to track it.

我希望开发部门也跟踪svn/trunk 像这样:

I want that the development branch will also track the svn/trunk something like:

from:
local branch > development branch

to:
local branch > development branch > svn/trunk

可以用git吗?

我正在使用 git版本2.14.3

I'm using git version 2.14.3

git GUI-smartgit v18.1

git GUI - smartgit v18.1

推荐答案

否,这在Git中是不可能的.但这也不是那么重要(真的!),因为在Git中跟踪分支只是意味着一个分支将另一个集合设置为其上游:

No, this is not possible in Git. But it's also not that important (really!), because tracking a branch in Git just means that the one branch has the other set as its upstream:

git branch --set-upstream-to=desired-upstream my-branch

您的每个分支机构随时可以最多一个上游.当分支确实具有上游时,会发生以下情况:

Each of your branches can have at most one upstream at any time. When your branch does have an upstream, the following occur:

    没有参数的
  • git fetch知道哪个其他Git存储库可能包含要获取的提交.因此,您可以运行git fetch而不是git fetch remote.

  • git fetch with no arguments knows which other Git repository may contain commits to be fetched. Hence you can run git fetch instead of git fetch remote.

但是,如果您只有一个名为origin的远程服务器(这很常见),那么这实际上并不会给您带来任何好处,因为如果git fetch没有参数,则从上游获取的任何内容都不会从origin中获取.换句话说,无论如何,git fetch已经从(单个)正确的远程获取.因此,在这里上游没有买到任何东西.

But if you only have one remote named origin, as is pretty common, this doesn't really buy you anything since git fetch with no arguments will fetch from origin if it has nothing better from an upstream. In other words, git fetch already fetches from the (single) correct remote anyway. So having an upstream here did not buy you anything.

git mergegit rebase在没有其他参数的情况下运行会自动知道要使用的远程跟踪名称. 确实为您买了东西,但是如果您想拥有两个上游,您仍然必须告诉git mergegit rebase 要使用哪个上游,因此必须键入git merge fred/xyzzygit merge giselle/xyzzy并不会真的很糟糕.实际上,它可能更好:git merge upstream1git merge upstream2的键入较短,但是fred/xyzzy是上游#1还是上游#2?为什么不使用更难忘的名称?

git merge and git rebase run with no additional arguments automatically know which remote-tracking name to use. This does buy you something, but if you'd like to have two upstreams, you would still have had to tell git merge or git rebase which upstream to use, so having to type git merge fred/xyzzy and git merge giselle/xyzzy isn't really much worse. In fact it's probably better: git merge upstream1 and git merge upstream2 are shorter to type, but is fred/xyzzy upstream #1, or is it upstream #2? Why not use the more memorable name?

像上面的其他两个命令一样,git pull(仅运行其他两个命令,首先获取,然后获取其他两个命令之一)也知道默认情况下使用什么.我建议大多数Git用户避免 git pull,因为这本来是一种便捷命令,但在最坏的情况下往往会带来不便.如果您避免使用git pull,则知道一切都不会为您带来任何好处.

Like the other two above, git pull—which just runs the other two, fetch first and then one of the other two commands—also knows what to use by default. I recommend that most Git users avoid git pull as it's meant to be a convenience command, but tends to be inconvenient at the worst times. If you avoid git pull, having it know things buys you nothing at all.

使用simple的标准push.default设置,默认的git push行为会更好. 确实为您买了东西,但您无论如何应该只应该推送到另一个Git存储库,因此您可以选择该特定的其他Git存储库作为分支的上游(单个).

The default git push behavior, using the standard push.default setting of simple, is better. This does buy you something, but you probably should only be pushing to one other Git repository anyway, so you can pick that particular other Git repository as the (single) upstream for the branch.

最后,git status命令(一些其他的Git命令运行或运行的一部分)变得更有用,它告诉您在所选上游之前和之后有多少个提交.这也包括git branch -v.我发现这具有重要的价值.

Last, the git status command (which some other Git commands run, or run part of) becomes more informative, telling you how many commits you are ahead of and behind the chosen upstream. This includes git branch -v as well. I find this has significant value.

幸运的是,有一种简单的方法可以从命令行获取相同的信息,并且您可以创建一个别名或脚本来为您执行此操作.当git status说您领先1"时,和/或低于3",则相当于运行:

Fortunately, there's a simple way to get the same information from the command line, and you can make an alias or script to do this for you. When git status says that you're "ahead 1" and/or "behind 3", it's running the equivalent of:

git rev-list --count --left-right <local-name>...<upstream-name>

例如,在分支stash-exporigin/master作为上游的情况下,我可以运行:

For instance, with branch stash-exp having origin/master as its upstream, I can run:

git rev-list --count --left-right stash-exp...origin/master

并获得相同的两个数字(作为原始数字):第一个是"ahead"计数,第二个是落后"数数.请注意此处的三个周期.;这种形式的命令和选项需要全部三个,而大多数命令大多使用两点语法.

and get the same two numbers (as raw numbers): the first is the "ahead" count and the second is the "behind" count. Note the three periods . here; this form of this command and options requires all three, while most commands mostly use the two-dot syntax instead.

总之,然后设置一个上游,以便某些本地分支名称跟踪某些远程跟踪名称或某些其他本地分支名称,其成本很低(一个命令),并且具有明显的好处,因此您应该在有意义的时候去做.但是好处不是很大,以至于需要拥有多个上游.

In summary, then, setting an upstream, so that some local branch name tracks either some remote-tracking name, or some other local branch name, has a very low cost (one command) and some noticeable benefits, so you should do it when it makes sense. But the benefits are not so huge as to need to have more than one upstream.

这篇关于如何使用git设置分支的多个跟踪级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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