Get started Bring yourself up to speed with our introductory content.

How to use Git's 'set upstream push' command

Have you recently cloned a GitHub or GitLab repository, created a new branch and tried to push back, only to encounter Git's fatal "Current branch has no upstream branch" error?

If that's the case, you'll need to use the git push --set-upstream command to link the new local branch to the remote repository.

Alternatively, you could use a special Git setting to automatically set upstream branches and eliminate the need to ever again use the git push --set-upstream command. That push.autoSetupRemote setting is discussed at the end of this article.

How to use the 'git push --set-upstream' command

To push a new local branch named dev to a remote repository, use one of these two git set upstream command syntaxes:

  1. git push --set-upstream origin dev
  2. git push –u origin dev

The –u switch is simply an abbreviation of the full-form --set-upstream Git command.

Syntax of the 'git push upstream' command

The exact syntax of the "push --set-upstream" command in Git will vary based on your environment. In this git push --set-upstream example, here is how the syntax works:

  • dev is the name of the new branch to be pushed
  • origin is the name used to identify the remote repository

Check your config files if you're unsure of the name Git uses to identify your remote repository.

Screenshot of the ‘git push --set-upstream’ fix for ‘no upstream branch’ error.
Use the ‘git push --set-upstream’ command to fix the fatal 'no upstream branch' error.

What does the '--set-upstream' command do in Git?

The git push --set-upstream command tells a remote repository to create a new branch to support the new branch and commit history that is about to be pushed to it.

After performing a push with a --set-upstream, go to your remote repository and you'll notice that a new branch has been created.

This enables remote repos such as GitHub or GitLab to stay synchronized with local changes to your code.

Fix upstream branch errors with autoSetupRemote

If you constantly run into Git's fatal "Current branch has no upstream branch" error, and you find issuing the git push --set-upstream command constantly to be annoying, you can tell Git to use the set-upstream option automatically whenever you push a new branch to the server.

This is done by setting the push.autoSetupRemote option in any of Git's configuration files.

How to set push.autoSetupRemote

To tell Git to automatically create new branches in remote repositories upon a push, simply issue the following command:

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

With that configuration setting in place, you'll never have to push with the git push --set-upstream command ever again.

Cameron McKenzie has been a Java EE software engineer for 20 years. His current specialties include Agile development, DevOps and container-based technologies such as Docker, Swarm and Kubernetes.

View All Videos
App Architecture
Software Quality
Cloud Computing