Git Upstream Branch and Updating a Feature Branch
1. What is a tracking/upstream branch?
What is an Upstream Branch?
An upstream branch is the remote branch that your local branch is configured to track.
It tells Git:
“This is the remote branch associated with my current local branch.”
For example:
Local branch Remote branch
------------- -------------
feature/login ----------> origin/feature/login
tracks
Here:
feature/login→ local branchorigin→ remote repository nameorigin/feature/login→ remote-tracking branchorigin/feature/loginis the upstream of the localfeature/loginbranch.
Why do we need an upstream branch?
Without an upstream branch, you may need to specify the remote and branch explicitly:
git push origin feature/login
and:
git pull origin feature/login
Once the upstream relationship is configured, you can simply use:
git push
and:
git pull
Git already knows which remote branch should be used.
Simple Example
Create a local feature branch:
git switch -c feature/login
Push it to the remote and configure its upstream:
git push -u origin feature/login
The -u means:
--set-upstream
After this:
Local Remote
feature/login ------------> origin/feature/login
tracks
Now these commands are enough:
git push
git pull
How to Check the Upstream Branch
Use:
git branch -vv
Example:
* feature/login abc123 [origin/feature/login] Add login
main def456 [origin/main] Update homepage
This shows that:
feature/login
↓ tracks
origin/feature/login
Important Distinction
Do not confuse upstream branch with the main branch.
If you are working on:
feature/login
its upstream is usually:
origin/feature/login
not:
main
main may be the branch from which your feature branch was created, but that does not automatically make main its Git upstream.
Key Points
- An upstream branch is the remote branch associated with a local branch.
- It simplifies
git pushandgit pull. git push -u origin <branch>pushes the branch and sets its upstream.git branch -vvshows tracking relationships.- A common relationship is:
main → origin/main
feature/login → origin/feature/login
2. How do you update your feature branch with the latest changes from main?
Suppose you created feature/login from main.
Initially:
main
A ---- B
\
C ---- D
feature/login
Meanwhile, other developers add new commits to main:
A ---- B ---- E ---- F main
\
C ---- D feature/login
Your feature branch does not automatically receive commits E and F.
You need to bring the latest main changes into your feature branch.
There are two common approaches:
- Merge
- Rebase
Method 1: Merge main into the Feature Branch
First, update your local main:
git switch main
git pull
Now switch back:
git switch feature/login
Merge main:
git merge main
Complete flow:
git switch main
git pull
git switch feature/login
git merge main
After merging:
A ---- B ---- E ---- F
\ \
C ---- D ------ M
feature/login
M represents a merge commit when one is required.
Your feature branch now contains the latest changes from main.
Method 2: Rebase the Feature Branch onto main
Another option is:
git switch main
git pull
git switch feature/login
git rebase main
Before:
A ---- B ---- E ---- F main
\
C ---- D feature/login
After rebase:
A ---- B ---- E ---- F ---- C' ---- D'
main feature/login
Git takes your feature commits and reapplies them on top of the latest main.
This creates a cleaner, linear history, but the rebased commits get new commit identities.
Merge vs Rebase
| Merge | Rebase |
|---|---|
| Combines histories | Replays commits on a new base |
| May create a merge commit | Creates linear history |
| Does not rewrite existing feature commits | Rewrites the rebased commits |
| Often safer for already-shared branch history | Common for cleaning/updating private feature work |
git merge main |
git rebase main |
For interview purposes, you can explain:
To update my feature branch, I first get the latest
main, switch to my feature branch, and then either mergemaininto it or rebase the feature branch ontomain, depending on the team's Git strategy.
Key Points
- Feature branches do not automatically receive new commits from
main. - First make sure you have the latest
main. - Merge approach:
git switch main
git pull
git switch feature/login
git merge main
- Rebase approach:
git switch main
git pull
git switch feature/login
git rebase main
- Merge preserves the existing branch history.
- Rebase gives a more linear history but rewrites the rebased commits.
- Be careful rebasing a branch that other developers are already using, because rewriting shared history can cause synchronization problems.