Git protip: automatically setup remote tracking on push
By Maxime Bréhin • Published on 30 January 2023 • 1 min

If you’re a command line user, you probably noticed that Git does nothing on the very first push of a branch. Instead, it prints a message that suggests to explicitly type the following command: git push --set-upstream origin <branch-name>.

$ git push

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

    git push --set-upstream origin demo

To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.

It tells Git where to push your branch:

  • what remote repository (yes, you can have multiple remote repos),
  • what branch name.

It also tells Git to setup a (default) tracking between your local branch and the remote branch you’re pushing to (then you’ll be able to use the shorter git pull and git push later on, without specifying the remote repo or branch).

This is quite constraining, especially since 99% of the time we only have a single remote repository and use same-name branches.

What if I told you that Git can now automatically manage the remote branch creation and tracking with a simple git push?

This is what the push.autoSetupRemote configuration option is for (available since Git 2.38, October 2022). You can set it up globally:

$ git config --global push.autoSetupRemote true

Then:

$ git push

To http://remote-repository-url.tld
 * [new branch]      demo -> demo
branch 'demo' set up to track 'origin/demo'.

Want more tips and tricks?

We’ve got a whole bunch of existing articles and more to come. Also check out our 🔥 killer Git training course: 360° Git!

Would you like to go one step further and fully master Git core concepts, or to get advice on how to guarantee the quality of your Git projects? We can help or train you - just tell us what you need!