2

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?

Inian
  • 80,270
  • 14
  • 142
  • 161
Karl Thompson
  • 65
  • 1
  • 7
  • Is `origin/` the parent branch? – Inian Feb 03 '17 at 18:01
  • Yes. `origin/` would be the parent branch of ``. So running the alias would merge `` into ``. – Karl Thompson Feb 03 '17 at 18:04
  • Why not a simple merge from `` when present in ``? – Inian Feb 03 '17 at 18:06
  • That's what I'm doing currently, but the place I work has fairly lengthy branch name prefixes, so if there was a way to just use `git sync` instead of `git merge origin/`, it would be convenient. – Karl Thompson Feb 03 '17 at 18:10
  • `git` doesn't really have any way to do this in general. You might be able to do it in certain limited cases if you make assumptions about branch origins. But, given arbitrary branches `A` and `B`, all `git` can really say about them is either they have one or more common ancestors, or they do not - there's nothing stored about whether `A` was branched from `B` or whether `B` was branched from `A`, or maybe both were branched from `C`, which has since been deleted... – twalberg Feb 03 '17 at 20:18
  • Ah. That's too bad. Thanks for the response. – Karl Thompson Feb 03 '17 at 20:46
  • https://stackoverflow.com/questions/3161204/find-the-parent-branch-of-a-git-branch – Josh Lee Feb 03 '17 at 21:12

1 Answers1

5

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, e.g.: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.


1You 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, e.g., 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.

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

torek
  • 448,244
  • 59
  • 642
  • 775
  • 1
    You can just as easily track local branches. – jthill Feb 03 '17 at 22:06
  • @jthill: yes, I thought about mentioning that, but it seemed a bit off-target for his particular use. I'll add it to footnote 1 though. – torek Feb 03 '17 at 23:01