cron job that does git fetch: is it thread safe?

Viewed 31

To save myself the annoyance of having to git fetch (I work on multiple repos every day) I am creating a cron job that runs git fetch in each of my repos. I would like to do it every 15 minutes. So it's possible that git fetch will be run at the same time as I am doing other git operations (add, commit, rebase, etc.) Can that cause issues?

#!/bin/bash

for repo in <repos>; do
    pushd $HOME/$repo
    git fetch
    popd
done

date >> /var/cache/git-fetch-all-repos/fetch-times
1 Answers

Assuming a default configuration of the remote refs being stored in refs/remotes/$remotename/$ref, no, there won't be a problem. git fetch will only affect refs stored in the refs/remotes namespace, while all local operations update the refs/heads namespace.

Two caveats though:

  1. Tags always go to refs/tags, so creating tags with the same name at the same time as fetching could pose problems. Git will let you know with a warning.
  2. git push will also update the refs/remote namespace. Pushing the same branch that has changed on the remote while fetching it might also give you non-deterministic behavior.

PS. Your script doesn't need Bash nor pushd/popd, Git can be instructed to use a different directory from the current working directory: for repo in $repos; do git -C "$HOME/$repo" fetch; done

Related