24

Firstly, I'm aware of a number of similarly worded questions, eg:

None of them (AFAICT) has an answer that matches my version of this question.

My situation is:

$ git status
# On branch stable
nothing to commit (working directory clean)
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 857 commits.

In the existing questions the accepted and upvoted answers mostly concur that it means literally what it says... I'm ahead and I need to push my new commits to origin/master.

I know that actually the opposite situation is true, that my local master branch is behind the remote origin/master and actually I need to git pull origin master before doing some work on it locally. (or possibly just git fetch origin ?)

My question is... is there some reason for the message to be worded Your branch is ahead of 'origin/master' by 857 commits. such that it literally makes sense?

Because the way I understand it at the moment the meaning is the opposite of what the message says ('my branch' is behind origin/master).

Or does it really mean: "The HEAD of the remote master branch is ahead of your local origin/master tracking branch" ?

update FWIW I am working in a team of half a dozen other developers. We all pull, commit and push etc many times a day without problem. I don't have a bug here... I'm just trying to understand why Git words its message this way - whether the wording itself is badly chosen, or if there's some underlying concept of Git that causes them to word it this way and which I'm not understanding properly.

more info
here is what I guess may be the relevant part of output from git config -l

remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
remote.origin.url=https://code.google.com/a/google.com/p/xxxxx/
branch.master.remote=origin
branch.master.merge=refs/heads/master
branch.master.mergeoptions=--no-ff
Community
  • 1
  • 1
Anentropic
  • 32,188
  • 12
  • 99
  • 147
  • 1
    If git is really lying to you about your local repo being 857 commits ahead of whatever repo you have set as the remote "origin", then it's true that none of the questions you linked will help you, but it probably also means you have some horribly broken git installed. This seems unlikely. – Wooble Jun 14 '12 at 12:10
  • 1
    why don't you `git fetch` and check it yourself instead of guessing? Maybe someone did a non-forward push or something. The thing is... someone screwed the repo, or your clone is screwed or you found a bug (but it's rare and i always tend to blame the user first). – KurzedMetal Jun 14 '12 at 12:12
  • 1
    Git is not "lying" to you, you really are 857 commits *ahead* of origin/master or your repo is seriously broken. How do you know you're behind origin/master and not ahead as git says? – user229044 Jun 14 '12 at 12:40
  • I don't think git is lying to me and I don't have a horribly broken repo. I think the message is telling me something true, but with misleading wording. What is definitely true is I haven't made new commits on my local `master`. I believe my local master is *behind* origin/master because when I `git pull origin master` it retrieves changed files, I assume from the remote repo. – Anentropic Jun 14 '12 at 13:06
  • try `git remote update` and then look @ the git DAG with `gitk`. probably what happened is either a bad rebase; either someone else pushed to the remote or that happened to your local repo. – Abe Voelker Jun 14 '12 at 13:43
  • Have a look at your repository with `gitk --all .`. Might make you wiser. – Thorbjørn Ravn Andersen Jun 14 '12 at 13:44
  • 1
    @abe yes obviously someone else pushed to the remote, I already know this. So to my mind the remote is then *ahead* of my local branch, no? If someone pushed new commits to the remote, why does it tell me that 'Your branch is ahead of origin/master' unless the words 'Your branch' are used in a non-intuitive way? – Anentropic Jun 14 '12 at 14:16
  • a second downvote, could someone tell me what is wrong with this question? – Anentropic Jun 14 '12 at 14:17
  • Please post the results of `git config -l`. – CB Bailey Jun 14 '12 at 14:30
  • @Anentropic I would suggest doing what Thorbjørn said, try `gitk --all`. Your `master` branch should show up ahead the `remotes/origin/master` branch. You need to look at the DAG to find out what's going on. – Abe Voelker Jun 14 '12 at 14:31
  • @charles I have posted the git config in the question now – Anentropic Jun 14 '12 at 14:59
  • @abe thanks, I've since pulled and carried on working but next time it happens I will check gitk for clues. – Anentropic Jun 14 '12 at 14:59
  • Can you post the output of `git show-ref`? It is possible you have something ambiguous that is matching `origin/master`. – CB Bailey Jun 14 '12 at 15:17

5 Answers5

27

You are over thinking this. The message isn't saying the remote is behind. It's saying your local repository's recorded commit for 'origin/master' is. At no point in generating that message did git communicate with a remote. It simply looked into the .git directory, and returned the contents of .git/refs/remotes/origin/master. Try it yourself. Both of these commands should return the same thing from the top-level of the repository:

cat .git/refs/remotes/origin/master
git rev-parse origin/master

The second command is simply a plumbing command for finding the 'origin/master' pointer. You could replace 'origin/master' with any branch to get the most recent commit on that branch.

That message is telling you is that your local 'master' is ahead of the commit returned by 'git rev-parse origin/master' by 857 commits. How did this situation arise? I can't say exactly, but I'd put considerable money on you accidentally merging a different branch into 'master'. Every time I've seen this problem, it's a bad merge from user error.

First, issue git fetch origin to make sure that 'origin/master' pointer is up-to-date. Then, study your git log. Look for something recent you don't expect. Download a program like tig or use gitk to get a visual graph of your commit history. It's a good bet you accidentally issued git pull stable while checked out on 'master'. Or something similar.

Christopher
  • 42,720
  • 11
  • 81
  • 99
  • 3
    Thanks, I think you have helped me find what had happened... I had merged a branch into `master` but I hadn't pushed that change. In the meantime others had pushed their changes to the remote, so when I pulled it looked like I was resolving everything by doing so. – Anentropic Jun 14 '12 at 16:55
  • 2
    IMO, this message is a perfect example of why git goes out of its way to confuse its users. Christopher is saying that the message means the exact opposite of what it literally says, and to explicitly ignore the advice that the message gives you. UI fail. – legalize May 24 '15 at 04:28
5

git rebase -p

It will say First, rewinding head to replay your work on top of it... and you'll be all set, because there's no work to replay.

andy magoon
  • 2,889
  • 2
  • 19
  • 14
  • 4
    I will explain why I think this was originally down voted. I was unsure of the syntax and tried copying 'git rebase -p' straight to the terminal, which obviously won't do anything. Instead, you need 'git rebase -p origin/master' – SullX Jun 24 '13 at 21:05
  • 1
    Would be a more helpful contribution if you explained what 'git rebase -p' would do... – ChrisV Dec 06 '13 at 10:46
2

I had this question too. I searched a bit and found out that, we occasionally run 'git pull upstream master' to fetch the latest changes from upstream master to our local master which is a forked branch. However these updated changes are not pushed to our remote master yet. Hence the message says 'our local is x commits ahead of remote master'. Before proceeding with new code or modification in forked branch environment, it's better to run the following commands

git checkout master_branch;
git pull upstream master;
git push

Ashys
  • 21
  • 1
2

I had the same issue. Like the chosen solution points out, I think the problem was the origin/master pointer being out-of-date. I mostly only do git pull origin master and not fetch.

Since I know I didn't have local changes, I hard reset to master and then fetched to update the pointer, and finally pulled to catch up on the commits on the remote master branch. Like this:

git reset --hard origin/master
git fetch origin
git pull origin master

Hope this helps someone in the future too.

Edit: also useful On this other question, I found a great solution with useful commands. These worked for me when the above didn't!

If you get this message after doing a git pull remote branch, try following it up with a git fetch. (Optionally, run git fetch -p to prune deleted branches from the repo)

Fetch seems to update the local representation of the remote branch, which doesn't necessarily happen when you do a git pull remote branch.

dko
  • 197
  • 8
1

In the end, the Actual solution for the question to correct the problem:

It was because it needed a git push, after a merge. This just happened to me too, same error message. Andy magoon was right also, because when I did the push, I see a clean slate now, with no bytes having been pushed. However, rebasing is not usually the best course.

    $ git status ./
On branch master-blah1
Your branch is ahead of 'origin/master-blah1' by 869 commits.
  (use "git push" to publish your local commits)
nothing to commit, working directory clean


    $ git push
    Counting objects: 7, done.
    Delta compression using up to 48 threads.
    Compressing objects: 100% (7/7), done.
    Writing objects: 100% (7/7), 653 bytes | 0 bytes/s, done.
    Total 7 (delta 4), reused 0 (delta 0)

$ git status ./
On branch master-blah1
Your branch is up-to-date with 'origin/master-blah1'.
nothing to commit, working directory clean
blamb
  • 4,220
  • 4
  • 32
  • 50