← Back to Git Course | Chapter 4: Remote Repositories | Lesson 5 of 11

GitHub Remote Branch

Remote-tracking branches like origin/main are Git's way of remembering what a remote repository looked like at your last fetch, which is what makes it possible to tell how far your local branches have diverged before you pull or push.
Syntax
bash
git fetch remote_name
git branch -r
git log remote_name/branch_name

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?

bash
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

bash
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

bash
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

bash
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

bash
git checkout -b feature-x origin/feature-x
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Treating origin/main as an editable branch and committing to it, when it is a read-only bookmark you cannot commit to directly.
  2. Expecting origin/main to be up to date without running git fetch, so it shows old information.
  3. Confusing local main with origin/main, where the two can differ until you merge or pull.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.