简体   繁体   English

git可以知道您当前分支的哪个分支吗?

[英]Can git know which branch your current branch was branched from?

I'd like to create a git alias that lets me sync my branch from the original branch it was branched from. 我想创建一个git别名,使我可以从分支的原始分支同步我的分支。

For example, if I branched from <branch A> , is there a way for a git alias to recognize that it needs to pull from origin/<branch A> in order to sync? 例如,如果我从<branch A>分支出来,是否有一种git别名可以识别它需要从origin/<branch A>提取以便进行同步的方法?

Git does not know or care how a branch name , like master or feature-31415 or ourDevelopmentProcessUsesReallyLongBranchNamesBecauseTheyHateUs , came about. Git不知道或不在乎分支名称 ,例如masterfeature-31415ourDevelopmentProcessUsesReallyLongBranchNamesBecauseTheyHateUs ,因为它们是ourDevelopmentProcessUsesReallyLongBranchNamesBecauseTheyHateUs All it knows or cares about is that the name exists and currently points to some particular commit. 它所知道或关心的只是该名称存在,并且当前指向某个特定的提交。 That said, though, every branch can have one upstream setting. 也就是说,尽管每个分支都可以有一个上游设置。

The upstream that you configure is taken as the merge argument if you run git merge with no arguments, or to use as the <upstream> argument for git rebase if you run that with no arguments, and so on. 如果运行不带参数的git merge ,则将配置的上游作为merge参数,如果不带参数运行的<upstream>用作git rebase<upstream>参数,依此类推。 Typically the upstream for master is origin/master . 通常, master的上游是origin/master You could set sbr (short branch) to have origin/anotherLongNameBecauseTheyWantToReplaceUsWithRobots as its upstream, and never have to type that again. 您可以将sbr (短分支)设置为origin/anotherLongNameBecauseTheyWantToReplaceUsWithRobots因为它们是上游,因为它们不再需要再次键入。

To set origin/zorg as the upstream for evil : origin/zorg设置为evil的上游:

git branch --set-upstream-to=origin/zorg evil

and to remove the upstream: 并删除上游:

git branch --unset-upstream evil

The upstream setting is simply a two-part field: 上游设置只是一个分为两部分的字段:

$ git config --get branch.master.remote
origin
$ git config --get branch.master.merge
master

This is how and why master has origin/master as its upstream. 这就是masterorigin/master作为其上游的方式和原因。 1 The upstream is also used automatically by git status , and is the default push target for git push , and is the result of applying @{upstream} to the branch name. 1上游的git status也会自动使用它,它是git push的默认推送目标,并且是对分支名称应用@{upstream}的结果。 This is all built in to Git. 这全部内置在Git中。 The catch is that you get only one upstream per branch. 问题是每个分支只能获得一个上游。 If that's all you need, you are good. 如果这就是您所需要的,那就太好了。

You can add your own configuration items, eg: 2 您可以添加自己的配置项目,例如: 2

$ git config branch.master.zorblax giant-space-eel

but nothing in Git itself will use this. 但是Git本身不会使用它。 Still, Git is a giant tool-set, not just a predigested solution that can never be used for anything outside the immediate imagination of its developers: you can write your own code that uses the zorblax setting, if you like. 尽管如此,Git是一个庞大的工具集,而不仅仅是一个简化的解决方案,永远不能用于其开发人员无法想象的任何事情:如果愿意,您可以编写使用zorblax设置的自己的代码。


1 You can set a local branch as the upstream of another local branch. 1您可以将本地分支设置为另一个本地分支的上游。 Configuration-wise, this sets the remote part to . 在配置方面,这会将remote部分设置为. , which the rest of Git then ignores when necessary, eg, for showing the @{upstream} setting symbolically. ,其余的Git会在必要时忽略,例如,以符号方式显示@{upstream}设置。

There is also a hidden detail here having to do with branch name mapping, when remote is set to the name of a remote. 当将remote设置为remote名称时,这里还有一个隐藏的细节与分支名称映射有关。 This is because the original value of the merge setting predates the invention of remotes. 这是因为merge设置的原始值早于遥控器的发明。 Normally this makes no difference, though. 通常,这没有什么区别。

2 President Zorblax . 2 佐布拉克斯总统 Note date of comic, more than ten years ago. 注释漫画的日期,十多年前。 I suspect the red-button text is newer, though.... 我怀疑红色按钮的文字是较新的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM