5

I do see other similar questions, but I dont really understand why this happens. For now, I am using git reset --hard HEAD then manually adding back my changes. The suggested solution seems to be git pull origin master? But I still get the same message after that. Whats wrong? How do I do a merge? I am still relatively new to GIT

$ git pull origin master
Nodester!
Enter passphrase for key '/home/jiewmeng/.ssh/id_rsa': 
From nodester.com:/node/git/jiewmeng/10267-f62c0a21d1a9d75ab7b6ace5858921d0
 * branch            master     -> FETCH_HEAD
Merge made by the 'recursive' strategy.

$ git branch -a
* master
  remotes/origin/master
Jiew Meng
  • 84,767
  • 185
  • 495
  • 805
  • Can you post the output of your `git pull` command? And also of `git branch -a`? – lindelof Jun 10 '12 at 05:16
  • How many remote repos do you have? Use `git remote` to list the remotes. If there are more than one, have you setup `master` to track some other remote than `origin`? This is the most common scenario of getting the 'branch is ahead by X commits' message. – Sailesh Jun 10 '12 at 05:40
  • @lindelof, I posted my `git pull` & `branch` output – Jiew Meng Jun 10 '12 at 06:21
  • possible duplicate of [Why is Git telling me "Your branch is ahead of 'origin/master' by 11 commits." and how do I get it to stop?](http://stackoverflow.com/questions/277077/why-is-git-telling-me-your-branch-is-ahead-of-origin-master-by-11-commits-a) – jscs Jun 10 '12 at 21:26

4 Answers4

9

As mentioned in "Why is Git telling me “Your branch is ahead of 'origin/master' by 11 commits.” and how do I get it to stop?"

"your branch is ahead by..." => You need to push to the remote master.
Run "git diff origin/master" to see what the differences are between your local repository and the remote master repository.

If you're ahead of the remote repo by one commit, it's the remote repo that's out of date, not you.
Pulling wouldn't help.

Now check also if you are actually on a branch (and not on a detached head).
This is your case here (you are indeed on master branch)

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • If I dont rememeber wrongly, I think that error is preventing me from doing a push ... tho I am not entirely sure ... have already `reset --hard HEAD` ... anyways, what you said makes sense, I'll keep that in mind – Jiew Meng Jun 10 '12 at 06:27
  • @JiewMeng if you aren't able to push, that could be another question in its own. – VonC Jun 10 '12 at 06:31
5

"Branch is ahead by X commits" can have 2 reasons: 1) You have real local commits, and you will need to do 'git push' 2) Your 'origin' branch is out of syn with remote end. Do:

git fetch

(Root cause appears to be linked to doing 'git pull origin master' instead of 'git pull')

FractalSpace
  • 5,577
  • 3
  • 42
  • 47
0

Branch is "ahead" by X commits means your local repo have some new commit that doesn't exist on remote repo. Try 'git fetch' first.

taiansu
  • 2,735
  • 2
  • 22
  • 29
0

I investigated same issue in my env and found these facts:

Origin/master is mark(label) on commit where I performed clone. I didn't create any commits on my side, just pulling without rebase from origin. This caused syncing my side of repo but didn't move or update origin/master position. Same situation appears if you perform push (origin/master label moves to your push position) and then do some pulls which fetch some new commits.

git log --graph --oneline -X master

where X is number >= number of commits "ahead" you can see after git status call. You can see in log result where is positioned origin/master mark. This internal git mark is used for calculating status message we are afraid of.

Hash of the commit is stored in refs/remotes/origin/master file. If you delete this file the origin/master mark disappear and also information about ahead commits will no longer be displayed.

If you want to sync origin/master mark to current master HEAD just type

git update-ref refs/remotes/origin/master cac0cab538b970a37ea1e769cbbde608743bc96d

where instead of my hash put your hash of the head from:

git log -1

or use copy/paste on file content if you wish. The result should be the same.

This helped me with the message, but I don't know what else was also affected. As far as I know this message has just informative meaning and its not an error or warning. Everything works for me as expected without any limitation.

Jan Stanicek
  • 1,201
  • 1
  • 14
  • 30