Push a New Git Branch to Remote and Set Upstream Tracking
Create a local branch (git branch or git checkout -b), then push and set tracking with git push -u origin <branch>. Verify with git branch -vv. Check tracking.
How do I push a new local branch to a remote Git repository and set up tracking?
I need to:
- Create a local branch from another branch using either
git branchorgit checkout -b - Push the local branch to the remote repository and make it trackable so that
git pullandgit pushwork correctly
Create the branch locally with git branch or git checkout -b, then push and set tracking with a one-liner like git push -u origin <branch> so git pull and git push work without extra arguments. If the remote branch already exists (or you need to change tracking), use git branch --set-upstream-to=origin/<branch> <branch> or git push --set-upstream origin <local>:<remote>.
Contents
- Quick commands
- Create a local branch with git branch or git checkout -b
- Push the local branch and set upstream (git push -u origin)
- If the remote branch already exists or you need a different remote name
- Verify tracking and common troubleshooting
- Examples and short workflows
- Best practices for branch naming and tracking
- Sources
- Conclusion
Quick commands
When you want the short answer, these commands do the job:
# create and switch to a new branch (from current branch)
git checkout -b my-feature
# push and set upstream so `git push` / `git pull` work by default
git push -u origin my-feature
# alternative (explicit long option)
git push --set-upstream origin my-feature
The -u / --set-upstream flag tells Git to set the branch’s upstream (tracking) to the remote branch you pushed, so future git push and git pull can run without specifying names. See the canonical Git guide for details: https://git-scm.com/book/en/v2/Git-Branching-Remote-Branches.
Create a local branch with git branch or git checkout -b
You asked specifically for creating a branch using git branch or git checkout -b. Both work; the difference is that git branch only creates the branch, while git checkout -b creates it and checks it out.
Examples:
# create (but stay on current branch)
git branch my-feature
# then switch to it
git checkout my-feature
# or create and switch in one step
git checkout -b my-feature
If you need to base the new branch on another branch (not the current HEAD), give the starting point:
# create new branch 'my-feature' based on 'develop'
git checkout -b my-feature develop
(Modern Git also has git switch -c my-feature—same idea. But using git branch or git checkout -b matches your requirement.)
Push the local branch and set upstream (git push -u origin)
To push a newly created local branch to the remote and make it trackable in one step:
git push -u origin my-feature
What this does:
- uploads your local branch
my-featureto the remote calledorigin(creatingorigin/my-feature) - sets the local branch’s upstream to
origin/my-featuresogit pullandgit pushcan be run without branch names
You can use the long form too:
git push --set-upstream origin my-feature
After a successful push you’ll usually see a message like:
“Branch ‘my-feature’ set up to track remote branch ‘my-feature’ from ‘origin’.” (example outputs are shown in many walkthroughs — see https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/git-push-new-branch-remote-github-gitlab-upstream-example).
If you prefer to push the current branch without typing its name, you can run:
git push -u origin HEAD
That pushes the current branch and sets its upstream to the remote branch with the same name.
For the canonical explanation of -u and upstream behavior, see the Git Book: https://git-scm.com/book/en/v2/Git-Branching-Remote-Branches and a concise FAQ entry at https://www.git-tower.com/learn/git/faq/set-upstream.
If the remote branch already exists or you need a different remote name
Two scenarios:
- Remote branch already exists and you want your local branch to track it without pushing:
# create local branch based on origin/feature and switch to it
git checkout -b feature origin/feature
# or create the branch and explicitly set upstream
git branch --set-upstream-to=origin/feature feature
# shorthand:
git branch -u origin/feature feature
- You want the remote branch to have a different name than your local branch:
# push local 'my-feature' to remote 'remote-name' and set upstream
git push -u origin my-feature:remote-name
Or, if you already pushed but didn’t set upstream, set it afterward:
git branch --set-upstream-to=origin/remote-name my-feature
The above --set-upstream-to / -u forms are handy when you need to change or correct tracking after the fact. GeeksforGeeks and the Git Book both summarize these options: https://www.geeksforgeeks.org/git/how-to-set-upstream-branch-on-git/ and https://git-scm.com/book/en/v2/Git-Branching-Remote-Branches.
Verify tracking and common troubleshooting
How to verify:
git branch -vv— shows local branches and their upstreams, plus ahead/behind counts.git status— will say “Your branch is up to date with ‘origin/my-feature’.” or show ahead/behind.git remote show origin— shows which local branches track which remote branches.
Common problems and fixes
- “No upstream configured for branch …”: Fix by running
git push -u origin <branch>orgit branch -u origin/<branch> <branch>. - “failed to push some refs” / non-fast-forward: remote has commits you don’t. Usually do:
git fetch origin
git rebase origin/<base-branch> # or `git pull --rebase`
git push
Rebasing keeps history linear; merge is an alternative if you prefer.
- Permissions / authentication errors: check
git remote -v, confirm the remote URL and credentials (SSH keys or token).
If you’re unsure which branch is being pushed, check git rev-parse --abbrev-ref HEAD to get the current branch name and then push with git push -u origin HEAD.
For quick Q&A and community examples on pushing and tracking, see the Stack Overflow thread that collects common workflows: https://stackoverflow.com/questions/2765421/how-do-i-push-a-new-local-branch-to-a-remote-git-repository-and-track-it-too.
Examples and short workflows
Example: Typical feature flow
# start from main
git checkout main
git pull origin main
# create and switch to feature branch
git checkout -b feature/login-ux
# work, add commits...
git add .
git commit -m "Start login UX"
# push and set upstream
git push -u origin feature/login-ux
Now teammates can fetch origin/feature/login-ux and you can git pull / git push without naming branches.
Example: Track an existing remote branch locally
# fetch remote branches first
git fetch origin
# create a local branch that tracks origin/their-feature
git checkout -b their-feature origin/their-feature
# or:
git checkout --track origin/their-feature
Both create a local branch set to track the remote branch immediately.
Best practices for branch naming and tracking
- Use clear branch names:
feature/,fix/,chore/prefixes help (e.g.feature/auth-token). - Push early and set upstream (
git push -u) so CI and collaborators can see your work. - Keep branches short-lived and rebase or merge regularly to avoid large conflicts.
- If you prefer explicitness, set upstream with
git branch --set-upstream-toafter pushing; that makes your intent clear. - Consider configuring
push.default = currentor usinggit config --global push.default simpledepending on your workflow, but only after you understand the behavior.
Sources
- Git - Remote Branches
- How to Set Upstream Branch in Git — Git Tower
- Stack Overflow — push a new local branch and track it
- Git push new local branch to remote — TheServerSide example
- Setting Upstream Branch in Git — GeeksforGeeks
- Git (RU) - Удалённые ветки
Conclusion
Create the branch locally with git branch or git checkout -b, then push and set tracking in one step with git push -u origin <branch> to make git pull and git push work automatically. If the remote branch already exists or you need to change tracking, use git branch --set-upstream-to=origin/<branch> <branch> or git push -u origin local:remote to achieve the same result.