Pushing your Git Branches to a Matching Remote Branch
One of the most annoying things about working with branches in Git is managing to push to the upstream repository without receiving the error:
$ git push
fatal: The current branch new has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin feature/new-thing
Yes, you could remember to run git push -u origin feature/new-thing
or git push --set-upstream origin feature/new-thing
, or as I do, have a handy shell alias for it, but life's too short.
It turns out that you can make Git push automagically just by changing default push behaviour. To configure it globally, run:
git config --global push.default current
Or add the following to your ~/.gitconfig
:
[push]
default = current
This can be added on a per-repo basis by running the below command, if you don't want it to be set globally:
git config push.default current
Note that by running git push -u
, Git will push to the matching current branch, and set your local branch configuration to track against the upstream, so your git status
will show details like how many commits are different between the two - super useful!