Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

Like the book says, they're moved automatically whenever git does any network operations. So that would include git fetch, git push, and git pull.

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.

I think you're a bit confused about what is meant by "moved automatically". When remote branches are moved, they are moved to reflect the current state of the remote repository. The remote repository itself isn't changed by git fetch or git pull, so the remote branches don't move either.

Try running git log --oneline --graph --decorate to see a nice graph of the current state of your repository. Note the position of branch-x and origin/branch-x in that graph. Then, run git push to push your changes up to the remote repository. Then run git log --oneline --graph --decorate again; you'll see that origin/branch-x has moved. That's what is meant in the quote you referenced.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top