GitHub Remote Branch
In this page:
What is a Remote-Tracking Branch?
A remote-tracking branch like origin/main is a read-only local bookmark that records where a branch on the remote pointed as of your last fetch -- it isn't a branch you commit to directly, it just reflects the remote's state so Git can compare it against your local work.
Example: What is a Remote-Tracking Branch?
git fetch origin
git log origin/main
Listing Remote Branches
git branch -r lists only remote-tracking branches, while git branch -a lists local and remote-tracking branches together, which is useful for seeing every branch that exists on the remote even if you haven't checked any of them out locally yet.
Example: Listing Remote Branches
git branch -r
git branch -a
origin/main and the Local main Branch
The local main branch and the remote-tracking origin/main branch are separate references that can drift apart -- comparing them with git log shows commits made locally that haven't been pushed yet, or commits made by others that haven't been pulled yet.
Example: origin/main and the Local main Branch
git log main..origin/main
git log origin/main..main
Fetch vs Pull
git fetch downloads new commits from the remote and updates remote-tracking branches like origin/main without changing any local branch or the working directory, while git pull does the same fetch and then immediately merges (or rebases) those commits into the current branch.
Example: Fetch vs Pull
git fetch origin
git pull origin main
Checking Out a Remote Branch Locally
Checking out a branch that only exists on the remote, using git checkout -b <name> origin/<name> or git switch -c <name> origin/<name>, creates a new local branch that starts tracking the remote branch, so future plain git pull and git push commands know exactly where to sync.
Example: Checking Out a Remote Branch Locally
git checkout -b feature-x origin/feature-x
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: