THE GITHTML FIELD GUIDE
Set a Git branch’s upstream deliberately
A branch upstream records which remote branch a local branch tracks. Use git branch --set-upstream-to for an existing remote-tracking branch, or git push --set-upstream when publishing a branch intentionally. Verify the relationship with git branch -vv. Tracking is configuration, not synchronization, and does not itself merge or download any commits.
Identify both sides of the relationship
Start by naming your local branch and the remote branch it should follow. These names often match but do not have to. The upstream relationship helps Git report ahead and behind counts and supplies defaults for certain operations. Push behavior also depends on configuration, so do not assume every push will always use the tracking branch exactly as you expect.
For example, a local fix/retry branch should usually track your published fix/retry branch, not origin/main merely because main is the destination of a future pull request. The review base and branch upstream serve different purposes. Tracking main could make routine pull behavior surprising while providing misleading comparisons for your contribution workflow.
Configure an existing remote-tracking branch
Fetch from the verified remote so the reference exists locally. The example assumes both the local branch and origin/fix/retry already exist. Setting the upstream does not switch branches or alter files. If the remote branch does not yet exist, decide whether you are ready to publish instead of inventing a tracking reference.
- Confirm that the remote owner and branch name are the intended collaboration destination. Similar branch names across forks can hide a wrong relationship.
- Inspect ahead and behind information only after the appropriate fetch; tracking a branch does not make the displayed remote state continuously current.
git branch --show-current
git fetch origin
git branch --set-upstream-to=origin/fix/retry fix/retry
git branch -vvTreat publication as a separate decision
When a new branch is ready to share, an explicit git push --set-upstream origin fix/retry can both publish it and establish tracking. Review the staged and committed content, remote URL, and repository visibility before doing so. This is a network write and should not be used merely to silence an error when you are not ready to upload work.
After configuration, inspect the next pull or push target if anything is unfamiliar. You can change or remove an incorrect upstream without deleting the branch’s commits, but first understand why it was set. A useful completion check records the local branch, remote alias, remote branch, and latest observed commit. Those four facts make synchronization decisions clear and distinguish a configuration repair from a history change.
Tracking configuration does not reserve a branch name on the server or prevent another contributor from updating it. Continue fetching before decisions that depend on the remote branch’s current position.
Sources and further reading
AI-assisted writing with source-linked guidance and illustrative examples. Read our editorial approach or report a correction.