3

I'd like to set the default upstream branch of any branch I create locally to be origin/main.

This is mainly for pull --rebase, since we never push to remote (only through a code review process).

Right now I achieve this by:

$ git checkout -b some-new-branch-name
$ git branch --set-upstream-to=origin/main

It's OK (I have a Bash alias to do that for me), but I was wondering whether I can set git's config to do that automatically.

So far I looked through the git-config documentation and StackOverflow questions but haven't found a solution.

Is this possible?

Amos Shapira
  • 3,540
  • 5
  • 27
  • 33
  • Even if you can't automate it, you can streamline it by setting the upstream as part of the branch creation - `git checkout -b some-new-branch-name --track origin/master` – Amber Mar 28 '19 at 02:54
  • Possible duplicate of [Why do I need to do \`--set-upstream\` all the time?](https://stackoverflow.com/questions/6089294/why-do-i-need-to-do-set-upstream-all-the-time) – wesinat0r Oct 30 '19 at 19:48

2 Answers2

5

You can set the branch.autoSetupMerge option to true to have Git automatically enable --track (which is equivalent to setting the upstream) for branches that are created from remote refs.

However, true is actually the default value for this setting - the reason you're likely not seeing its effect is because you're actually creating branches that start from the local copy of main.

If you create your branches with something like:

git checkout -b new-branch-name origin/main`

Then then would automatically track origin/main.

Amber
  • 446,318
  • 77
  • 595
  • 531
  • 1
    Thanks, that could be a solution but it means that I now have to type `git checkout -b new-branch-name origin/whatever-base-branch-I-want` instead of the simple `git checkout -b new-branch-name` I have now. Not much of an improvement. BTW for a couple of our repositories the "main" branch has a different name, it would be nice if I could just tell git "track whatever the original branch tracked". – Amos Shapira Mar 29 '19 at 03:24
1

You could use a post-checkout hook to set the upstream branch https://schacon.github.io/git/githooks.html

EncryptedWatermelon
  • 3,042
  • 10
  • 26