1

How can we know if the local master is behind remote master in repo with out pulling or fetching.

I mean before pulling or fetching, I would like to determine if my local master is behind repo master so that I can perform fetch / rebase / merge if it is behind

Venu
  • 1,513
  • 3
  • 19
  • 37

3 Answers3

2

No way that I know of. If you fetch, you don't have to rebase immediately and then "git status" will tell you if you're ahead or behind (or both). You'd have to do some kind of "remote query" to get the information you want - and fetch is that query.

daf
  • 1,289
  • 11
  • 16
1

git fetch is for this very particular purpose. It updates the tracking branches so that you know how far/behind your branch is as compared to remote branch. It does not merge/apply the changes in the remote to your local branch. git pull does that.

And that is the difference between git pull and git fetch

Pankaj Singhal
  • 15,283
  • 9
  • 47
  • 86
  • What happen to "without pulling or fetching"? "before pulling or fetching"? – VonC Jun 07 '16 at 06:29
  • I think the person asking the question was confused about the working of `git fetch` and `git pull`. So, I just thought of clarifying it because `git fetch` would solve the problem in the question. – Pankaj Singhal Jun 07 '16 at 07:35
  • No, the person was *not* confused: the question was: "*without* fetching". You did not answer the question. – VonC Jun 07 '16 at 07:47
  • Why would anyone want to over complicate things when the same could be done in a simpler manner? – Pankaj Singhal Jun 07 '16 at 09:38
  • That is a legitimate question that you are asking. It is *not* the one asked by the OP though. – VonC Jun 07 '16 at 10:24
  • Let's agree to disagree. All OP wanted to do was to know whether his local branch was _behind_ of the remote one so that _He can perform fetch / rebase / merge if it is behind_. You went with the _words_ of OP and chose to provide a solution which _you thought_ he wanted. Whereas I chose to provide a solution which was simplistic and what he needed. – Pankaj Singhal Jun 07 '16 at 18:28
  • I was not confused. I want to check if my branch is behind or not and if behind, then only run the fetch /merge / rebase commands. Else, do nothing. – Venu Jun 09 '16 at 07:23
  • @VonC the above comments is the proof that the OP was confused – Pankaj Singhal Jun 09 '16 at 09:22
  • @Rams the confusion we are talking here is that whether you deliberately wanted to not do `fetch` for knowing that your branch was behind the `remote` branch. – Pankaj Singhal Jun 09 '16 at 09:24
1

You still have to contact the remote repo in some way.

The only command that can give you an hint is git ls-remote

git ls-remote origin

More precisely, compare:

git ls-remote origin -h refs/heads/master 
git rev-list --max-count=1 origin/master

If the SHA1 returned for the master branch differs from the one you have locally, your branch might be behind (or ahead if that SHA1 is accessible from your local branch HEAD)

At least, with git ls-remote, you don't fetch all repo.

Pankaj Singhal
  • 15,283
  • 9
  • 47
  • 86
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250