Next.js starter your AI actually understands. Ship internal tools in days not weeks. Pre-order $199 $499 → [Get it now]

How to set upstream branch in Git

Setting an upstream branch establishes a tracking relationship between your local branch and a remote branch, allowing you to use git push and git pull without specifying the remote and branch name. As the creator of CoreUI with 25 years of Git experience managing distributed teams, I use upstream branches daily for streamlined workflows.

The most effective command is git push -u origin branch-name which pushes and sets upstream in one operation.

Direct Answer

Use the -u flag when pushing to set upstream:

git push -u origin feature-branch

After Creating a New Branch

When you create a new local branch:

git checkout -b feature-login
git push -u origin feature-login

Set Upstream for Existing Branch

If you already pushed without -u:

git branch --set-upstream-to=origin/feature-branch

Short form:

git branch -u origin/feature-branch

Push and Set Upstream

For the current branch:

git push --set-upstream origin $(git branch --show-current)

Default Push Behavior

Configure Git to automatically set upstream:

git config --global push.autoSetupRemote true

Now git push automatically sets upstream on first push.

Check Upstream Status

View which branches have upstream configured:

git branch -vv

Output shows tracking information:

* feature-login  a1b2c3d [origin/feature-login] Add login form
  main           d4e5f6g [origin/main: ahead 2] Latest changes

Remove Upstream

Unset upstream tracking:

git branch --unset-upstream

Set Different Upstream

Track a different remote branch:

git branch -u origin/develop

Explanation

The upstream branch is the default remote branch for fetching and pushing. Setting it once with -u means you can use git push and git pull without arguments. Git stores this configuration in .git/config as branch.<name>.remote and branch.<name>.merge.

Best Practice Note

This is the same upstream workflow we use in CoreUI’s repositories with hundreds of contributors. Setting upstream on the first push with -u saves typing and prevents errors from pushing to the wrong branch. The push.autoSetupRemote config (Git 2.37+) automates this completely.

For related Git operations, check out how to prune remote branches in Git and how to change remote URL in Git.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
What Does javascript:void(0) Mean?
What Does javascript:void(0) Mean?

What is JavaScript Array.pop() Method?
What is JavaScript Array.pop() Method?

How to loop inside React JSX
How to loop inside React JSX

How to replace all occurrences of a string in JavaScript?
How to replace all occurrences of a string in JavaScript?

Answers by CoreUI Core Team