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!

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

Tags

This post was filed under articles.

Related Posts

Other posts you may be interested in:

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.