According to Pro Git (3.5):
[remote branches are] local branches that you can’t move; they’re moved
automatically whenever you do any network communication.
However, this doesn't seem to be correct in practise. I've arrived at a situation where I get
Your branch is ahead of 'origin/branch-x' by 23 commits.
but I haven't actually done any commits, just fetched /pulled modifications that other people have pushed to origin. If the statement of Pro Git was correct I would expect my remote branch to be the same as my local one, since every fetch or pull would be a communication with origin.
What are the exact operations that update the remote branches?
I suspect your issue might be rooted in a special twist git pull
has in its semantics — let's cite the git pull
manual:
Some short-cut notations are also supported.
...
A parameter <ref> without a colon is equivalent to <ref>: when pulling/fetching, so it merges <ref> into the current branch without storing the remote branch anywhere locally
This means that when you do
git pull
or
git pull origin
(which rely on the branch.<name>.remote
and also branch.<name>.merge
— in the latter case — configuration variables) then all the branches the remote repo has are fetched and the remote branches in your local repository are updated accordingly.
But when you do git pull origin branch-x
, then the objects of the branch "branch-x" in the repo "origin" are fetched1, no remote branch is updated in your local repo, and then the fetched tip object is merged into the currently checked out branch.
So to fix your situation just run git fetch origin
and have your remote branches updated.
You might want to consider reading this blog post to get a better idea of why git pull
is not a precise equivalent to git fetch
+ git merge
in all the possible cases.
Another way to fix the situation for the origin/branch-x
remote branch is to do git push origin branch-x
assuming your local branch-x
is set to track origin/branch-x
— this happens because when you push a tracking branch Git knows the receiving branch in the remote repo now looks exactly like the branch you've just pushed and so it's a sane thing to update the matching remote branch.
If you do not feel yourself comfortable with these concepts (remote branches vs tracking branches) start from reading this part of The Book.
1 Well, actually only the objects which are missing in your local repository are fetched.