some confusion on git pull vs git-fetch

1.2k views Asked by At

So - can someone clarify this one:

I run:

git pull origin master  
git status

And it then pulls the changes and says:

your branch is ahead of origin/master ... blahblah by 6 commits...

When I then run

git fetch
git status

It says:

# On branch master
nothing to commit (working directory clean)

So - I thought git pull does git fetch by default - so why does it says "ahead by 6 commits" without additional git fetch?

1

There are 1 answers

3
Ash Wilson On BEST ANSWER

The "ahead or behind by X commits" text in git status is based on the state of the tracking branch for the current branch; remotes/origin/master if you're on master, for example.

When you run git pull with both a remote and a branch specified, it fetches the new commits and merges them in to the current branch, but it does not update origin's remote tracking branches. Instead, it points to the just-fetched commits as FETCH_HEAD.

Running git fetch with no arguments specified, on the other hand, does update all of the remote tracking branches, so it makes the message go away. git pull with no arguments does the same.

A subtle gotcha that I've hit a bunch of times myself! I wish git updated all remote tracking branches on every fetch against a particular remote, instead.