Usually, there are three different kinds of branches you have to consider:
- A branch in a remote repository
- A remote branch in your local repository
- A local branch in your local repository
The work you do is usually done on local branches. These are the normal branches that you create and check out. E.g. when you do git branch foo
or git checkout -b foo
, you create a local branch. Local branches are the easiest to understand.
Now, when talking about a remote repository, you usually don’t work with that directly. So instead, it has some local state itself. For example, when you push to a remote master
branch, then that remote repository has a master
branch on its own. That is the branch in the remote repository.
When you add a repository as a remote to your local repository, then Git will use remote branches to represent the state the remote repository is in. When you run git fetch
, Git will fetch the branch information from the remote and update its remote branches. Remote branches are of the pattern <remote-name>/<branch-name>
.
So if a remote repository origin
has a local branch master
, then when fetching that remote, your local repository will create a remote branch origin/master
. That remote branch exists within your local repository and represent the state of the master
branch in the remote.
Now, when you also have a local branch master
, you can set up that branch to track the remote branch. That means that you will link master
and origin/master
so that pushing and pulling knows exactly which remote and which branch on the remote they have to interact with; and it will also give you that information when you switch to the branch.
However, it’s important to keep in mind that Git, being a distributed version control system where almost everything is executed locally, will only look at the remote branch when checking the state of your local branch. It will not automatically connect to the remote repository to check its local state of the branch.
So when Git says “Your branch is up to date with 'origin/master'” then what it means is “Your local branch and the remote branch that exists locally currently point at the same commit but the remote branch could possibly be stale”. To fix that, you will first have to run git fetch
to update the remote branches in your local repository so that they reflect the new current state of the remote repository.