Sander van Dragt's Notes

@

Any developers using bash/zsh shell and git can use this code snippet to effectively git fetch when entering a directory:

function chpwd {
  GIT_ROOT=$(git rev-parse --show-toplevel 2>/dev/null)

  if [ -n "$GIT_ROOT" ]; then
    FETCH_STAMP_FILE="$HOME/.cache/chpwd_git_fetch_$(echo "$GIT_ROOT" | tr '/' '_')"
    if [ ! -f "$FETCH_STAMP_FILE" ] || [ $(find "$FETCH_STAMP_FILE" -mmin +10) ]; then
      ({ git fetch --all --quiet && touch "$FETCH_STAMP_FILE"; } > /dev/null 2>&1 &)
    fi
  fi
}

This snippet acts like a lightweight background updater, seamlessly integrating with your shell navigation to keep your local metadata fresh, reduce manual fetches and out of date merges. #til