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.
Related Articles
For related Git operations, check out how to prune remote branches in Git and how to change remote URL in Git.



