2

On one machine git log show it does not get the latest commits from remote repo (after run git pull)

git log --oneline -n 3 origin/develop
5a8e6eb xxx
7547c8a xxx
53294b6 xxx

On another machine, the correct one, it gets the latest commit 4b45d4b

git log --oneline -n 4 origin/develop
4b45d4b (origin/gq_dev, origin/develop) xxx
5a8e6eb (tag: dev_12) xxx
7547c8a xxx
53294b6 (origin/zsf) xxx

What strange on the problematic machine is that, my guy told my he actually used git pull origin pull to get the latest commit 4b45d4b from origin. So when run git status it then shew "Your branch is ahead of 'origin/develop' by 1 commit."

git status
# On branch develop
# Your branch is ahead of 'origin/develop' by 1 commit.
...

git show
commit 4b45d4bfff7c54169fea7343c5b4f020be556d0a

So what did that happen and how do I fix it?

------- update -----------

I accidentally fixed it without knowing why, so I added write permission to anyone to .git/logs/refs/remotes/origin/develop file

Originally it was

ls .git/logs/refs/remotes/origin/develop
-rw-rw-r-- 1 gongqiang     gongqiang      2103 Jan 30 19:07 develop

After adding write permission and run git pull again the problem fixed! But I still don't know why.

----- update 2 ------

The answer I got from VonC cast some light on the issue but there are still questions left unanswered.

  1. ls .git/logs/refs/remotes/origin/develop shew only the guy gongqiang had write permission originally (b/c it was him who run git checkout the develop branch). It was also him that run git pull to get latest commit 4b45d4b.
  2. After I run git status said "Your branch is ahead of 'origin/develop' by 1 commit" I checked this file. It did NOT record latest pull. But since it was this guy own the file and run that git pull. Why git failed to record it?
  3. I change this file to write permission to anyone and run git pull again to fix it. But I really doubt this is the "correct" fix.

Another question raised from here is since this is a branch that more one person will git pull (the integration branch to do test). Is it better to use sudo git pull or change the write permission let other run git pull without sudo

---- update 3 -----

I accepted VonC answer and the link he provided in commits https://serverfault.com/questions/26954/how-do-i-share-a-git-repository-with-multiple-users-on-a-machine/27040#27040 is useful.

But I also needed to point out that it is probably impossible (or not worth the effort) to find out why the guy who owned .git/logs/refs/remotes/origin/develop failed to let git update it when he run git pull.

So a lesson I learned here is when finding git status said something strange, check with .git/logs/refs/remotes/origin/develop/branch to verify

Qiulang
  • 10,295
  • 11
  • 80
  • 129
  • did you try `git fetch` on the machine where you are not getting the latest commit? generally `git fetch` `git pull` should help. – Hiren Jan 31 '19 at 05:05
  • Yes I run git fetch & git pull. It didn't fix the problem. – Qiulang Jan 31 '19 at 05:08

1 Answers1

1

It could be a case of having run (as in here) a git pull as root in the past, changing the permission/ownership of some files.

To be sure to work on a coherent repo, simply clone it again, and check in that clone that git log --oneline -n 3 origin/develop does return the expected commits.

. But I wanted to know why git status said "your branch is ahead of 'origin/develop' by 1 commit." while we used git pull to get that commit from origin

I suppose the permission issue (that you fixed) prevented Git to record the latest SHA1 for origin/develop.
But: that latest commit was still fetched, and your develop branch was still fast-forwarded to said latest SHA1 (4b45d4b)

Since the local branch points to 4b45d4b, but .git/logs/refs/remotes/origin/develop still reference the previous commit (because it was not updated properly), you would get

# Your branch is ahead of 'origin/develop' by 1 commit.

Another question raised from here is since this is a branch that more one person will git pull (the integration branch to do test).
Is it better to use sudo git pull or change the write permission let other can just run git pull

As explained here, you can:

  • either change permissions of the group, then change the umask for the users to 002, so that new files get created with group-writable permissions.
  • or setting up the extended ACL for the group so that the group members can read/write/access whatever files are already there.
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • It was indeed running git pull as root, then changed the permission of some files. But I wanted to know why git status said "your branch is ahead of 'origin/develop' by 1 commit." while we used git pull to get that commit from origin – Qiulang Jan 31 '19 at 07:02
  • @Qiulang If it shows "your branch is ahead of 'origin/develop' by 1 commit.", that means the most recent commit is not yet pushed, or the remote branch is not yet fetched to reflect that last commit. – VonC Jan 31 '19 at 07:48
  • I guess you did not get my question. On the machine we used git pull origin develop to get latest commit from origin. Then we run git status on that machine, to our surprise it show "your branch is ahead of 'origin/develop' by 1 commit." – Qiulang Jan 31 '19 at 07:50
  • @Qiulang Again, that means the most recent commit is not yet pushed to origin/develop. – VonC Jan 31 '19 at 07:52
  • NO the most recent commit 4b45d4b was pushed to origin/develop. That is why on another machine, it can get it! – Qiulang Jan 31 '19 at 07:53
  • @Qiulang Are we talking about the same 4b45d4b though? Meaning, does the full SHA1 of origin/develop HEAD matches with 4b45d4bfff7c54169fea7343c5b4f020be556d0a? – VonC Jan 31 '19 at 07:57
  • Yes. And that was why I asked the question in the first place! – Qiulang Jan 31 '19 at 07:58
  • The problematic server is our staging server. It only git pull code from origin to do test. After we run git pull the latest commit 4b45d4b I run git status just to verify we did not change anything. Then to my surprise we find "Your branch is ahead of 'origin/develop' by 1 commit." – Qiulang Jan 31 '19 at 08:03
  • @Qiulang that should be a consequence of the permission issue, preventing that server to properly record the last commit fetched for `origin/develop`. – VonC Jan 31 '19 at 08:05
  • @Qiulang OK, I have edited the answer to address the additional question. – VonC Jan 31 '19 at 08:33
  • I still think there are question unanswered. Please check my update question. Actually I already guesses the reason (what you answered) when I change the file permission. – Qiulang Jan 31 '19 at 08:33
  • To be honest with you I believe my guy will just sudo git pull and it is probably impossible to (or not worth the effort) find out why the guy owned that file failed to let git update it. – Qiulang Jan 31 '19 at 08:39
  • @Qiulang I understand, but fixing the group permission as described in https://serverfault.com/a/27040/783 is cleaner. Plus, none of my user would have access to sudo for that command anyway. – VonC Jan 31 '19 at 08:44