MrDuk
MrDuk

Reputation: 18282

Trying to setup argumentless git push - how to configure origin as default upstream?

I have a development branch that I branch off of for specific features. I'd like to be able to create/checkout a branch, do some commits, and just issue:

git push

or

git push origin

to push to the remote repo. Either command is fine with me if possible, I just want to not have to specify my branch every time I push. I've tried setting up some config with:

git config --global push.default upstream

but when I try to do either of the above commands to push, I just get the error:

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

git push --set-upstream origin myBranch

Am I missing something?

Upvotes: 0

Views: 124

Answers (1)

poke
poke

Reputation: 387815

When you create new branches locally (using git branch or git checkout -b) those branches are just local branches and do not contain any tracking information. So when you push, they will not automatically push and as such create a remote branch, unless you explicitely tell them to do that.

One way to do that is already mentioned in the error message: git push --set-upstream origin remoteBranchName or git push -u origin remoteBranchName for short. After that, the upstream configuration for push.default (as well as the simple configuration becoming default in Git 2.0) will make git push push that branch.

You could also try setting push.default to current as that will always push the current branch to the remote branch with the same name. But I actually never tried that, and I would advise against it, as it removes the symmetry between push and pull that upstream (or simple) achieved.

Btw. if the remote branch already exists, but you don’t have a local branch, just checking out that remote branch (git checkout remoteBranchName) will automatically create a new local branch and set up the tracking information.

Upvotes: 2

Related Questions