5

Every time I create a branch and push it, this happens:

$ git push fatal: The current branch foo has no upstream branch. To push the current branch and set the remote as upstream, use

git push --set-upstream origin foo

$ git push --set-upstream origin foo

Given I am on branch foo, the whole '--set-upstream origin foo' feels like redundant typing (or rather, copy-pasting).

Is there a way I can set a config option so that 'git push' while on a branch that has no upstream automatically sets the upstream branch name to match the local branchname?

joachim
  • 28,554
  • 13
  • 41
  • 44

2 Answers2

3

The following alias in your gitconfig will work. The hard work of getting the alias right is due to this answer and this one, but they never say in the question or answer what they are trying to do.

    publish = !git push -u origin `git symbolic-ref HEAD | sed -e "s#^refs/heads/##"`

or

    publish = !git push -u origin `git branch | grep \\* | cut -d ' ' -f2`

Then a simple git publish does what you want.

Community
  • 1
  • 1
MikeF
  • 764
  • 9
  • 26
-1

Simple, create tracking branches

You should either use complete git push origin <remote_branch> notation or set up a tracking branch.

git branch --track branch-name origin/branch-name
git branch --set-upstream-to <remote-branch> # for existing branches

Now you can just git push and it would work.

hspandher
  • 15,934
  • 2
  • 32
  • 45