Not a Git Repository in Coding Agents, Containers and CI: Fixing Repository Context
fatal: not a git repository means Git cannot discover repository metadata from the current directory. For coding agents and automation, that usually signals a broken execution context: the agent started in the wrong workspace, the container mounted only a subdirectory, CI did not check out source, or a worktree path disappeared.
Do not run git init automatically. That can create a new repository inside the wrong directory and hide the real orchestration failure.
Prove the context before acting
pwd
git rev-parse --show-toplevel
git rev-parse --git-dir
git status --short --branch
If rev-parse fails, inspect the workspace supplied to the process. An agent should validate its repository root before reading, editing, staging, or committing files.
Containers and sandboxed agents
Mounting /workspace/src without repository metadata leaves source files but no Git context. Mount the intended working tree and preserve the .git file/directory needed by normal Git commands. With worktrees, .git may be a file pointing into the common repository; do not assume it is always a directory.
Avoid giving a container broad access to unrelated repositories. Give each agent one explicit worktree and verify:
- expected remote URL;
- expected branch or detached-HEAD policy;
- clean/dirty status;
- allowed path scope;
- commit and push permissions.
CI checkout failures
A CI job may run before its checkout step, in a different working directory, or in a later container that did not receive the workspace. Make repository validation an early failing step:
test "$(git rev-parse --is-inside-work-tree)" = "true"
git rev-parse --show-toplevel
Do not discover a missing checkout only after an agent has generated files elsewhere.
Safe recovery
- Stop the agent or job before it writes further.
- Confirm the intended repository, branch, and worktree.
- Restore the checkout or correct the working directory.
- Re-run status and remote checks.
- Preserve untracked work before recreating a disposable workspace.
- Resume from a reviewed state.
The Git foundation covers branches and worktrees. Connect agent execution to Coding Agents, CI checks to GitHub Actions, and failure monitoring to AI Operations.