home about me

git pushup: a little alias for easier branching

git branches are a lovely thing to keep your work tidy and easily collaborate with other developers. But if you want to avoid those pesky upstream error messages when trying to push a freshly created branch, there are two things you can do.

The Problem

If trying to push a branch that does not have a remote counter part yet, git push will give you an error message:

fatal: The current branch foobar has no upstream branch.

There are two easy ways to fix this:

Solution #1: git alias

git config --add alias.pushup '!git push --set-upstream origin $(git symbolic-ref --short HEAD)'

This simple command will setup an alias. Now, typing git pushup will invoke the command above. The exclamation mark at the beginning tells git that this is a shell command, and to execute it from the root directory of the current project. It can be freely used in place of git push.

Solution #2: config option autoSetupRemote

If you’re running the latest version of git, things become even easier:
git v2.37.0, released 2022-06-27, has added a new config option: push.autoSetupRemote
If set to true, git pushes will behave like we used our alias above. Simply do this to set the option globally:

git config --global --add --bool push.autoSetupRemote true