git push new branch

Git has changed its default push policy some time ago from ‘matching’ to ‘simple’, meaning that if you try to push a new branch for the first time, you will see this:

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

    git push --set-upstream origin my-new-branch

Now of course you can change your push policy configuration, but maybe you want to retain that default behaviour and only override it explicitly for some cases? Then this may help you: We can get the name of the current branch using git rev-parse --abbrev-ref HEAD and thereby construct a simple command alias like this (gpn standing for ‘git push new’):

alias gpn=git push --set-upstream origin `git rev-parse --abbrev-ref HEAD`

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.