0

Can someone please explain how this is possible that "git log -1" returns different hash on the same repository on different machines (repositories are fully synced)?

  • we have several docker containers that run cron jobs and periodically pull the repository (the containers don't push anything - just get latest)
  • each of them runs this command on startup:
   git clone --filter=blob:none --no-checkout --single-branch --branch master --depth 1 git@github.com:<our repository>.git /<destination-folder>/
   git sparse-checkout set /<repository-sub-folder>
   git checkout
  • there's a cron job that runs the following command every few minutes (only one container at a time):
   git pull --ff-only
   cd <repository-sub-folder>
   git log -1 -- .

The problem is that it becomes out of sync at some point and "git log" returns different commit ID when running from two different containers. Note that the state of remote repository is the same at this point and all containers pulled latest version from remote.

After I remove the local copy of repository and run initial script again (which clones remote repo in sparse mode) the issue doesn't happen.

Yury Kozlov
  • 1,286
  • 1
  • 11
  • 9

1 Answers1

1

git pull --ff-only may not work as expected if remote.<name>.fetch is not properly set or the repository is on detached HEAD.

In a script, it's better to use the full command explicitly like git pull origin <branch> --ff-only. When you omit something, Git tries to find out what it is from gitconfig. If gitconfig does not provide the info, the command could succeed but not as expected.

git pull origin refs/heads/<branch> --ff-only is better. Sometimes we may create ambiguous refs like a branch refs/heads/foo and a tag refs/tags/foo. Some hosting services even have self-defined refs under other categories. git pull origin foo may fetch the unexpected foo, while git pull origin refs/heads/foo and git pull origin refs/tags/foo don't bring such invisible problems.

ElpieKay
  • 27,194
  • 6
  • 32
  • 53
  • 1
    I recommend that scripts avoid `git pull` entirely: break it up into separate `git fetch` and `git merge --ff-only` steps so that you have control if and when either step fails. I doubt this is the source of the problem, but it may be contributing to hiding whatever the source is. – torek Mar 15 '22 at 08:30
  • @elpiekay thanks for the answer, the recommended fix seems to work. (I will continue monitoring) – Yury Kozlov May 15 '22 at 15:09