Back to Blog
Lesson 25 of the Git & GitHub: Git & GitHub from Zero course
GitAugust 12, 20264 min read

Understanding Remote Tracking Branches: A Practical Guide

Remote tracking branches are the bridge between your local machine and GitHub. Learn how Git maps your work to the cloud to simplify your workflow.

GitVersion ControlBranchingGitHubDevOps
Interior view of a ship's control bridge with navigation technology and machinery.

Previously in this course, we covered pushing code to GitHub and linking local repositories. Now that your code lives in the cloud, you need to understand how Git keeps your local environment in sync with that remote state.

When you work with a team or even just across multiple machines, your local repository needs a way to "remember" what the remote version looks like without constantly asking the server for updates. This is where remote tracking branches come in.

What is a Remote Tracking Branch?

A remote tracking branch is a local, read-only copy of a branch that exists on a remote repository. Think of it as a "bookmark" that Git maintains to remember where the remote branch was the last time you communicated with it.

You don't edit these branches directly. Instead, Git updates them whenever you interact with your remote (like running git fetch or git pull). By keeping these references locally, Git can calculate the differences between your current work and the remote work instantly, without needing a network connection.

The Origin/Main Relationship

When you set up a remote, you typically name it origin. If you have a branch named main on your local machine, Git creates a corresponding remote tracking branch named origin/main.

The syntax is always <remote-name>/<branch-name>. This relationship is often called the upstream. When you "push" or "pull," you are essentially synchronizing your local main with the origin/main reference.

Viewing Your Tracking Branches

Two parallel railway tracks stretching through a tranquil nature scene with trees and greenery.

You can see these hidden references by using the branch command with the -r (remote) flag.

Bash
git branch -r

You will see output similar to this:

TEXT
  origin/main
  origin/feature-login

To see both your local branches and their tracking status, add the -vv (verbose, verbose) flag:

Bash
git branch -vv

The output will look like this:

TEXT
* main    a1b2c3d [origin/main] Add initial project files
  feature-a 4e5f6g7 [origin/feature-a: ahead 1] Implement login logic

Here, the bracketed text [origin/main] confirms that your local main is tracking origin/main. The note [ahead 1] is a helpful hint from Git telling you that you have one commit locally that hasn't been pushed to the remote yet.

Hands-on Exercise: Inspecting Your Upstream

Let’s verify the tracking relationship in your current project.

  1. Open your terminal in your project directory.
  2. Run git branch -vv to view your current branch tracking configuration.
  3. If you don't see an upstream branch listed, try running git fetch to update your local view of the remote, then run the command again.
  4. Observe how your local branch maps to the remote.

Common Pitfalls

  • Forgetting to Fetch: A common mistake is thinking git branch -r shows the current state of GitHub. It only shows the state as of your last git fetch or git push. If a teammate pushed code five minutes ago, your tracking branch won't know until you update it.
  • Detached HEADs: If you ever try to git checkout origin/main directly, you will enter a "detached HEAD" state. Remember: you shouldn't commit directly to tracking branches. Instead, create a new local branch if you need to work on a feature.
  • Broken Upstream: If you ever find that git pull or git push isn't working because Git "doesn't know where to send the code," you may need to explicitly set the upstream using git push -u origin <branch-name>.

FAQ

Can I delete a remote tracking branch? Yes, but it won't delete the branch on GitHub. It only removes your local reference to it. If you need to clean up stale tracking branches, use git fetch --prune.

Do I need to manually link every branch? Usually, no. When you run git push -u origin <branch-name>, Git automatically sets up the tracking relationship for you.

Why does my terminal say 'behind'? This means your origin/main (the remote bookmark) is ahead of your local main. It's time to run git pull to bring your local work up to date with the remote.

Recap

Team members presenting a project in a modern office setting with a focus on collaboration.

Remote tracking branches are your local "snapshots" of your remote repository. They allow you to see the gap between your work and the team's work using git branch -vv. By understanding the origin/main relationship, you can confidently navigate, fetch, and synchronize your code without surprises.

Up next: We will dive into Introduction to Forking, where you'll learn how to contribute to other people's projects by creating your own remote copy.

Similar Posts