Alex Terreaux
Alex Terreaux

Reputation: 1941

Is it possible that git status tells you everything is up to date when actually it is not?

Yesterday, I merged some branches that I worked on during the weekend into master. Then I deleted every branch except for master. All of these changes were done during the weekend so the local repository in my work computer was outdated. Today I came to work and ran git status, and it said

On branch master
Your branch is up-to-date with 'origin/master'.

nothing to commit, working directory clean

However I was 100% sure my local repo at work was not up to date, because I made several changes. After running git pull It downloaded all my changes and everything worked fine.

My question is: Is it possible that under some circumstances git status can tell you you are up to date with remote when you are not? I am not sure if this is expected behavior or git just messed up.

Upvotes: 1

Views: 70

Answers (2)

gravetii
gravetii

Reputation: 9654

How does git know which branch to compare with exactly?

git does not do a diff with the actual remote repository. It keeps track of the diff by comparing your local branches to the corresponding remote references (origin/master for example). To get the correct (updated) git status, you need to fetch from the remote, which will update the remote references. You can do that by git fetch origin

git status will then give you the correct diff.

When you did a git pull, what you actually did is a fetch followed by a merge, which resulted in your local branch being brought up to date with the remote master. If you want, you can do just a git fetch and then git will tell you how many commits your local master is behind the remote one.

So you should always fetch from the remote before:

  1. creating a feature branch
  2. pushing to remote
  3. wanting to compare between local and remote branches

Upvotes: 3

nneonneo
nneonneo

Reputation: 179717

git status does not check the actual remote repository. It tells you if you are up-to-date with respect to the local copy of the remote repository. Just remember that origin is a cached copy of the remote which resides on your computer, and not the remote repository itself.

You have to git fetch to update this local cache, after which git status will tell you if you are behind or not.

Upvotes: 4

Related Questions