pull-only repo's 'git status' saying the branch is ahead of origin/master. Why?

本秂侑毒 提交于 2019-11-29 06:31:40
Nic

Ok, so from the outset, you're doing everything correctly. I think the comment you added previously is a pretty good explanation:

In the simplest terms, "git pull" does a "git fetch" followed by a "git merge"

That's how I think of it. So you shouldn't have to call a git fetch after a straight up git pull - but, I can almost guarantee you, this works perfectly fine on anything EXCEPT the master branch.

In one of the linked posts, it said to remove the following line:

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/* <--- Remove this

And it should fix this issue - however, I cannot provide an explanation as to why this works. It's very hard to research, but I think that when you call fetch, your git config actually specifies what exactly to grab. When you're running pull, I'm not sure that it thinks the master is synced.

I can guarantee you that if you did this from another non-master branch, you wouldn't see this problem. Hopefully one of the git gurus can explain the fetch line in config in detail.

Furthermore, I would recommend running the following command instead which sets the HEAD for the remote repository to ensure it is in sync with your local one: git push -u origin master


Here's another interesting question:

Having a hard time understanding git-fetch


Ok, so I tested this on one of my workflows and found the following.

When you execute a git pull origin master on your remote server, there's a file in the .git/ directory that references where your HEAD is at. Two files to take note of:

ORIG_HEAD

FETCH_HEAD

You'll notice that your FETCH_HEAD is correct, but the ORIG_HEAD shows the old commit, hence the reason you're getting the Ahead by x. When you run git fetch, you'll actually correct the reference in ORIG_HEAD and everything is back to normal. I'm looking into how to change the fetch line in the config to fix this behavior.

If you run a git pull origin instead of a git pull origin master, there won't be the issue with the Your branch is ahead of 'origin/master' by ... commits. message.

jhanifen

See this question: What is the difference between 'git pull' and 'git fetch'?

AFAIK a git pull will look at the branch on origin and pull down the changes. But the local index of the branch is not up-to-date. git fetch will update the index of the branch so it understands what should be there. (basically what was referenced in the answer you linked to)

I always do a git fetch before a git pull. Really I do a git fetch anytime I am going to be doing anything with remote branches.

Also linked on the above question is this very good description of git fetch, pull and merge. http://longair.net/blog/2009/04/16/git-fetch-and-merge/

Ulad Kasach

In my case - I had two branches in Origin and everytime i pulled a git pull it showed that i was ahead of origin/master by x. Even after hard reseting it to the origin/master like shown in Reset local repository branch to be just like remote repository HEAD.

The solution occured when i simply ran git fetch and it brought my development branch onto my production server.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!