简体   繁体   中英

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.

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?

Git does not know or care how a branch name , like master or feature-31415 or ourDevelopmentProcessUsesReallyLongBranchNamesBecauseTheyHateUs , came about. 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. Typically the upstream for master is origin/master . You could set sbr (short branch) to have origin/anotherLongNameBecauseTheyWantToReplaceUsWithRobots as its upstream, and never have to type that again.

To set origin/zorg as the upstream for 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. 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. This is all built in to 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

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

but nothing in Git itself will use this. 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.


1 You can set a local branch as the upstream of another local branch. Configuration-wise, this sets the remote part to . , which the rest of Git then ignores when necessary, eg, for showing the @{upstream} setting symbolically.

There is also a hidden detail here having to do with branch name mapping, when remote is set to the name of a remote. This is because the original value of the merge setting predates the invention of remotes. Normally this makes no difference, though.

2 President Zorblax . Note date of comic, more than ten years ago. I suspect the red-button text is newer, though....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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