git runtime_error ai_generated true

fatal: This operation must be run in a work tree

ID: git/detached-head-from-bare-repo

Also available as: JSON · Markdown · 中文
90%Fix Rate
85%Confidence
1Evidence
2024-02-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
git 2.43.0 active
git 2.44.0 active
git 2.45.0 active

Root Cause

Attempting to run a command that requires a working tree (like checkout or status) inside a bare repository.

generic

中文

尝试在裸仓库中运行需要工作树的命令(如checkout或status)

Official Documentation

https://git-scm.com/docs/git-worktree

Workarounds

  1. 95% success Clone the bare repo into a working tree: git clone /path/to/bare.git /tmp/workdir && cd /tmp/workdir && git status
    Clone the bare repo into a working tree: git clone /path/to/bare.git /tmp/workdir && cd /tmp/workdir && git status
  2. 85% success Use 'git worktree add' to create a linked working tree from the bare repo: git worktree add /tmp/workdir main
    Use 'git worktree add' to create a linked working tree from the bare repo: git worktree add /tmp/workdir main

中文步骤

  1. Clone the bare repo into a working tree: git clone /path/to/bare.git /tmp/workdir && cd /tmp/workdir && git status
  2. Use 'git worktree add' to create a linked working tree from the bare repo: git worktree add /tmp/workdir main

Dead Ends

Common approaches that don't work:

  1. Running 'git checkout main' in a bare repo thinking it will switch branches 95% fail

    Bare repos have no working tree; checkout cannot modify files

  2. Setting GIT_DIR to a bare repo path and running git status 90% fail

    Bare repos store only objects, no index or working tree for status