Interview Prep/Git & GitHub/Config, Line Endings, Files and Platform Quirks

A hook (or a script it calls) runs `git -C /srv/other-repo pull`, which fails with `fatal: not a git repository: '.git'` or operates on the wrong repo.

AdvancedTroubleshootingConfig, Line Endings, Files and Platform Quirks
Answer

Likely causes → Git exports GIT_DIR (and in some hooks GIT_INDEX_FILE, GIT_WORK_TREE) to hook processes. Child Git commands inherit them and ignore -C/the current directory, so they target the original repo or a relative .git that does not exist there.

Diagnose → Log the environment in the hook: env | grep ^GIT_ > /tmp/hook-env.txt.

Fix → Clear the variables before touching another repo:

#!/usr/bin/env bash
unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
git -C /srv/other-repo pull --ff-only

In a bare-repo post-receive deploy hook, set them deliberately instead: git --work-tree=/srv/app --git-dir="$PWD" checkout -f main.

Prevent → Treat hooks as running inside Git's environment; always reset GIT_* variables before invoking Git elsewhere.

All Git & GitHub questions Roadmap: Git & GitHub

More on Config, Line Endings, Files and Platform Quirks