Key concepts

Remote-tracking branch

If your repository is connected to a remote repository (for example, origin), you'll usually have two sets of branches:

  1. Local branches are the normal branches you work on.

  2. Remote-tracking branches are special read-only branches that reflect the state of the branches present in the remote repository. These branches are named using the format <remote-name>/<branch-name> (e.g., origin/main).

Why are they useful?

  • To compare local branches with the remote. You can use commands like git log or git diff against origin/main to see what has changed on the remote without touching your working tree.

  • To recover messed-up local branches. If your local branch goes off the rails, you can reset it to the state of a remote-tracking branch (e.g. git switch main followed by git reset --hard origin/main).

  • To create new branches. Need to start work based on something that only exists on the remote? Create a local branch from the remote-tracking branch: git switch -c feature origin/feature.

Where do they come from?

  • After cloning a repository, all branches from the remote repository are copied as remote-tracking branches into your local repository. However, only the default branch is checked out as a proper local branch.

    For example, if the remote has branches main, feature-1, and feature-2, your local repository will have remote-tracking branches origin/main, origin/feature-1, and origin/feature-2, but only main will be checked out as a local branch.

  • Running git fetch updates existing remote-tracking branches in your repository and creates new ones if new branches have been added to the remote.

    If a branch was deleted from the remote, the corresponding remote-tracking branch will still exist locally until you prune it by running git fetch --prune. Git never discards data without your explicit consent.
  • Running git pull is the same as running git fetch followed by git merge (or git rebase), so it also updates remote-tracking branches.

© 2024-2025 GitByBit.All rights reserved.