Remote-tracking branch
If your repository is connected to a remote repository (for example, origin), you'll usually have two sets of branches:
-
Local branches are the normal branches you work on.
-
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 logorgit diffagainstorigin/mainto 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 mainfollowed bygit 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, andfeature-2, your local repository will have remote-tracking branchesorigin/main,origin/feature-1, andorigin/feature-2, but onlymainwill be checked out as a local branch. -
Running
git fetchupdates 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 runninggit fetch --prune. Git never discards data without your explicit consent. -
Running
git pullis the same as runninggit fetchfollowed bygit merge(orgit rebase), so it also updates remote-tracking branches.