Git: Branch is ahead by X commits. Doesn't help doing git pull

后端 未结 4 1636
无人及你
无人及你 2021-01-03 03:16

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.

相关标签:
4条回答
  • 2021-01-03 03:28

    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)

    0 讨论(0)
  • 2021-01-03 03:38

    "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')

    0 讨论(0)
  • 2021-01-03 03:48

    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.

    0 讨论(0)
  • 2021-01-03 03:51

    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.

    0 讨论(0)
提交回复
热议问题