【完全ガイド】git fatal: not a git repository の原因と解決方法|.git探索順序・Docker/WSL/worktree対応まで徹底解説
- 作成日 2026.07.14
- git
Git を始めた初日、多くの人が遭遇する最初の壁:
$ git status
fatal: not a git repository (or any of the parent directories): .git
$ git log
fatal: not a git repository (or any of the parent directories): .git
$ git commit -m "..."
fatal: not a git repository (or any of the parent directories): .git
Git のほぼ全コマンドでこのエラー。「Git ってどうやって使うの?」と挫折しかけた経験がある方も多いはず。
しかも、Git 中級者になってからも予期しないタイミングで遭遇:
- Docker コンテナに入って git 使ったら
- ホームディレクトリで誤って git 実行
rm -rf .gitしてしまった- worktree が壊れた
- submodule 内で発生
- CI/CD で謎の発生
一見単純に見えて、実はGit の内部動作を理解しないと本当の意味が分からないエラーです。
現場では:
- どこにいるのか分からず混乱
git initすべきなのか、cdすべきなのか判断できない.gitの探索順序を知らない- Docker / WSL 特有の罠にハマる
- worktree での特殊挙動
本記事では、fatal: not a git repository の完全な原因と解決方法を、リファレンスとして実用的に整理します。エラーの本質、.git の探索順序(parent directories)、5つの発生原因、各パターン別対処、Docker / WSL 対応、worktree・submodule・bare repository の特殊ケース、実践シナリオ、予防のベストプラクティス、FAQまで完全網羅。この1本でこのエラーで挫折することはなくなります。
結論:ほぼ「ディレクトリ問題」
時間がない方向けに、最速の対処法を先に示します。
90%の場合の解決
# 1. 今どこにいる?
pwd
# 2. .git はある?
ls -la | grep .git
# 3. なければ init または cd
git init # 新規なら
# または
cd path/to/git-repo # 別ディレクトリに Git repo がある
発生原因 5選
| 原因 | 症状 | 解決 |
|---|---|---|
| ① git init 忘れ | 最初の使用時 | git init |
| ② ディレクトリ違い | 別の場所にいる | cd で移動 |
| ③ .git 削除 | 誤って消した | 再 init or clone |
| ④ Docker / WSL | 環境の問題 | volume マウント確認 |
| ⑤ worktree 壊れた | .git が壊れた file | git worktree repair |
診断コマンド
# 総合診断
git rev-parse --is-inside-work-tree
# true → Git repo 内、false or エラー → 外
# トップレベル確認
git rev-parse --show-toplevel
# 親を含めた探索
find . -name ".git" 2>/dev/null
ls -la .. | grep .git
詳細は以下で解説します。
まず理解する:.git ディレクトリの仕組み
Git repository の本質
Git repository = .git ディレクトリを持つフォルダ
my-project/
├── .git/ ← ここに全ての履歴・設定が入っている
│ ├── HEAD
│ ├── refs/
│ ├── objects/
│ └── config
├── README.md
└── src/
.git/ がなければ、Git にとっては**「ただの普通のフォルダ」**。だから Git コマンドが動かない。
エラーメッセージの分解
fatal: not a git repository (or any of the parent directories): .git
翻訳:
not a git repository: 「ここ、Git リポジトリじゃないよ」or any of the parent directories: 「親ディレクトリを遡っても見つからないよ」.git: 「.gitフォルダを探したよ」
.git の探索順序(重要)
Git は現在のディレクトリから、ルート(/)まで遡って .git を探します:
/Users/you/project/src/utils
↑
.git 探索
↓
/Users/you/project/src ← .git ある?
↓ ない
/Users/you/project ← .git ある?
↓ ある!
→ Git repo として認識、以降のコマンドが動く
サブディレクトリからでも Git コマンドが動くのは、この探索の仕組みのおかげ。
探索が失敗するケース
/Users/you/random-folder/
↓ .git ない
/Users/you/
↓ .git ない
/Users/
↓ .git ない
/
↓ .git ない
→ 探索終了、エラー
ルートまで遡って見つからなければエラー。
【原因①】git init 忘れ(初心者最頻出)
症状
# 新しいプロジェクト
mkdir my-app
cd my-app
echo "hello" > index.html
git status
# fatal: not a git repository (or any of the parent directories): .git
解決
git init
# Initialized empty Git repository in /path/to/my-app/.git/
git status
# On branch main
# ...
git init の確認
ls -la
# drwxr-xr-x 10 you ... .
# drwxr-xr-x 20 you ... ..
# drwxr-xr-x 10 you ... .git ← あれば OK
.git/ ディレクトリが作られる。以降のコマンドが動く。
続いて最初の commit
git init だけでは足りないこともあります:
git init
git log
# fatal: your current branch 'main' does not have any commits yet
→ 最初の commit も作る:
touch .gitkeep
git add .gitkeep
git commit -m "Initial commit"
詳細はfatal: bad revision ‘HEAD’ の記事も参照。
【原因②】ディレクトリ違い
症状
Git repo は別のディレクトリにあるが、間違った場所でGitコマンド実行:
pwd
# /Users/you ← ホームディレクトリ
git status
# fatal: not a git repository
診断
# 今どこ?
pwd
# 目的のプロジェクトはどこ?
find ~ -name ".git" -type d 2>/dev/null | head
# → プロジェクトの場所が判明
解決
cd ~/projects/my-app
git status
# → 動く
ディレクトリを間違えているが最頻出パターン。
VS Code / IDE でよくある間違い
VS Code で「開く」した場所が親ディレクトリだった
→ ターミナルが親で開く → Git repo 外
対処:
cd src/my-app # サブに移動
git status
【原因③】.git を誤って削除
症状
# 誤って .git を削除
rm -rf .git
git status
# fatal: not a git repository
解決A:再 init(履歴は消える)
git init
# 空の repo として再スタート
過去の commit 履歴は全て失われる。
解決B:Fresh clone(推奨)
リモートに push 済みなら:
# 別のディレクトリで clone
cd ..
mv broken-project broken-project.backup
git clone https://github.com/user/project.git
# バックアップから最新変更を戻す
diff -r broken-project.backup project/src/
# → 必要なファイルをコピー
リモートがあるなら fresh clone が最も安全。
解決C:ゴミ箱から復元(macOS/Windows)
削除直後なら OS のゴミ箱に残っている可能性:
- macOS: Trash から復元
- Windows: Recycle Bin
- Linux:
trash-cliを使っていれば
早めに確認。
【原因④】Docker / WSL 特有の問題
Docker container 内で発生
docker exec -it container bash
cd /app
git status
# fatal: not a git repository
診断
# container 内の /app に .git ある?
ls -la /app | grep .git
# volume マウント確認
mount | grep /app
解決
パターン1: ホストの Git repo が volume マウントされていない
# docker-compose.yml
services:
app:
volumes:
- .:/app # ✅ カレントを丸ごとマウント
# - ./src:/app/src # ❌ src だけだと .git がない
パターン2: dockerignore で .git 除外
# .dockerignore
.git # ← これがあると container 内から .git が見えない
必要なら削除。
パターン3: Container 内で init し直し
用途によっては container 内で新規 init:
docker exec -it container bash
cd /app
git init
詳細はdocker daemon 接続エラーの記事、Docker no space left on device の記事も参照。
WSL / Windows 混在
# WSL 内
cd /mnt/c/Users/YourName/project
git status
# 動くケースが多い
# 権限問題
git status
# fatal: detected dubious ownership in repository at ...
対処:
git config --global --add safe.directory /mnt/c/Users/YourName/project
CI/CD の Docker 内
# GitHub Actions
- uses: actions/checkout@v4
- run: |
docker run -v ${{ github.workspace }}:/app image
docker exec container git status
# → checkout がホストに配置される
# → volume でマウントされていれば OK
【原因⑤】worktree の破損
git worktree とは
同じリポジトリの複数ブランチを別ディレクトリで同時作業できる機能:
# main で作業しながら、feature を別ディレクトリで
git worktree add ../feature-branch feature
cd ../feature-branch
# → main を触らず feature 作業可能
worktree での .git
通常のリポジトリ:
project/.git/ ← ディレクトリ
worktree:
feature-branch/.git ← ファイル(!)
中身:
cat .git
# gitdir: /path/to/main-project/.git/worktrees/feature-branch
この .git ファイルが壊れるとエラー。
解決
# 修復
git worktree repair
# または再作成
cd ..
rm -rf feature-branch
git worktree add feature-branch feature
worktree 一覧確認
git worktree list
# /path/to/main abc1234 [main]
# /path/to/feature-branch def5678 [feature]
特殊ケース
submodule 内での発生
cd submodule-dir
git status
# fatal: not a git repository
対処:
# 親から init
cd ..
git submodule update --init --recursive
bare repository
bare repository はworking tree なし、.git/ の中身だけ:
# 通常
project/
.git/
README.md
# bare
project.git/
refs/
objects/
HEAD
bare repo 内で git status は動かない(working tree がないため)。
GIT_DIR 環境変数
export GIT_DIR=/path/to/other/repo/.git
git status
# → 環境変数の repo に対して実行
Git は $GIT_DIR を優先。設定漏れで意図しない場所に影響することも:
# 環境変数確認
env | grep GIT
git worktree で .git ファイル
worktree での .git はファイル(ディレクトリではない):
# worktree
ls -la .git
# -rw-r--r-- 1 you .git ← ファイル
これがおかしいと not a git repository。
診断コマンド完全リスト
現在位置確認
pwd
# 現在のディレクトリ
.git の存在確認
ls -la | grep .git
# .git/ があるべき
# 親も含めて探索
find . -name ".git" 2>/dev/null
find /path/to/parent -name ".git" 2>/dev/null | head
Git 内部診断
# Git 内かどうか
git rev-parse --is-inside-work-tree
# true / false
# Git のトップ
git rev-parse --show-toplevel
# .git のパス
git rev-parse --git-dir
# bare repository か
git rev-parse --is-bare-repository
環境変数
env | grep -i git
# GIT_DIR / GIT_WORK_TREE などが設定されていないか
実践シナリオ
シナリオ1:新しいプロジェクト開始
mkdir new-app
cd new-app
git status
# fatal: not a git repository
# 解決
git init
touch README.md
git add README.md
git commit -m "Initial commit"
詳細はerror: src refspec does not match any の記事、fatal: bad revision ‘HEAD’ の記事も参照。
シナリオ2:clone 済みプロジェクトが行方不明
pwd
# /Users/you
git status
# fatal: not a git repository
# 探す
find ~ -name ".git" -type d 2>/dev/null | grep -v node_modules
cd ~/found/project
git status
# → 動く
シナリオ3:Docker container 内で作業
docker exec -it web bash
cd /app
git status
# fatal
# 診断
ls -la | grep .git
# → .git がない
# docker-compose.yml の volume 確認
修正:
volumes:
- .:/app # .git 含めてマウント
シナリオ4:CI/CD で発生
- uses: actions/checkout@v4
with:
path: 'my-app' # サブディレクトリに配置
- run: git status
# → error, my-app 内に .git がある
- run: cd my-app && git status
# → OK
シナリオ5:Rails プロジェクトの復元
# .git を誤って削除
rm -rf .git
# GitHub から fresh clone
cd ..
mv my-app my-app.broken
git clone https://github.com/user/my-app.git
cd my-app
# 変更あれば手動で戻す
diff -r ../my-app.broken/ .
詳細はRails 8 アップグレードガイドの記事も参照。
シナリオ6:Kamal デプロイ用リポジトリ
# デプロイ用の作業ディレクトリ
cd deploy-workspace
git status
# fatal: not a git repository
# → 通常 Kamal は Git repo 内で動く
cd ../my-rails-app
kamal deploy
詳細はKamal 2 デプロイの記事も参照。
シナリオ7:VS Code で複数プロジェクト
VS Code Workspace:
├─ /Users/you/project-a (Git repo)
└─ /Users/you/project-b (Git repo)
問題: ターミナルが workspace root に開く
→ どちらの repo でもない
→ fatal: not a git repository
対処:
cd project-a # 明示的に移動
git status
シナリオ8:worktree の管理
# 複数ブランチを同時作業
git worktree add ../feature feature
# feature-branch に移動
cd ../feature
git status
# 通常は動く
# 動かない場合
git worktree repair
シナリオ9:submodule 内での作業
# メインリポジトリで
git clone https://github.com/user/parent.git
cd parent
git submodule update --init --recursive
# submodule 内で
cd submodules/vendor
git status
# → 動く(submodule init されていれば)
シナリオ10:チームのオンボーディング
# 新メンバーへ
echo "1. リポジトリを clone してから始めてください:"
echo " git clone https://github.com/team/project.git"
echo "2. project ディレクトリに移動:"
echo " cd project"
echo "3. これで git コマンドが使えます"
予防のベストプラクティス
1. まず clone、init は最小限
# ✅ 推奨:clone から始める
git clone https://github.com/user/repo.git
cd repo
# → 全部揃っている
# △ init は自作プロジェクトのみ
git init
2. pwd を体で覚える
Git コマンド実行前に pwd。ホームディレクトリで Git 使ってしまう事故を防ぐ。
3. git status を常用
git status
最も安全な Git コマンド。動かない = Git repo 外の証拠。
4. Shell プロンプトに Git 情報
zsh/bash のプロンプトに現在のブランチを表示:
# .zshrc に
autoload -Uz vcs_info
precmd() { vcs_info }
setopt PROMPT_SUBST
PROMPT='%~ ${vcs_info_msg_0_}$ '
zstyle ':vcs_info:git:*' formats '(%b)'
これでGit repo 内かどうか一目瞭然。
5. .git を意識的に
# 作業前に確認
ls -la | grep .git
削除しないよう注意。
6. Docker では volume マウント確認
# docker-compose.yml で .git 込みでマウント
volumes:
- .:/app
7. IDE の Terminal は project 内で
IDE のターミナルはプロジェクトルートで開く設定に。
トラブルシューティング
「dubious ownership」エラー
git status
# fatal: detected dubious ownership in repository at '/mnt/c/...'
WSL や共有マシンで発生。対処:
git config --global --add safe.directory /path/to/repo
# または全ディレクトリ許可(非推奨)
git config --global --add safe.directory '*'
.git があるのに動かない
ls -la
# .git ある
git status
# fatal
.git の権限確認:
ls -la .git
chmod -R u+rw .git
GIT_DIR 環境変数の影響
env | grep GIT_DIR
# → 変な値が設定されていないか
unset GIT_DIR
git status
.git がシンボリックリンク
ls -la .git
# lrwxrwxrwx ... .git -> /path/to/other
# リンク先が存在しないと壊れる
readlink .git
ls /path/to/other
対処:
rm .git
# 再作成 or fresh clone
詳細はLinuxでシンボリックリンクを作成・確認・削除する方法の記事も参照。
worktree の連鎖障害
メインリポジトリを削除すると、全 worktree が壊れる:
# メイン削除 → worktree で
git status
# fatal
対処:
# メインを再取得(可能なら)
git clone ...
git worktree repair
よくある質問(FAQ)
Q1. .git は隠しフォルダ?
. で始まるため隠しフォルダ扱い。ls -la で表示。
Q2. .git の中身は編集していい?
基本ダメ。Git コマンド経由で操作すること。壊すと復旧が困難。
Q3. Git repo をコピーしたい
cp -r project project-copy
cd project-copy
git status
# → 動く(.git ごとコピーされる)
Q4. .git を圧縮したい
git gc
# 内部を最適化
Q5. .git サイズが大きい
- 大きいバイナリファイルの履歴
- 不要な branch / tag
対処:
git gc --aggressive
# または git-filter-repo で履歴書き換え
Q6. サブディレクトリで Git 動く
動きます(探索の仕組みで親を遡る):
cd project/src/deep/dir
git status
# → 動く(project/.git を発見)
Q7. WSL で頻発する
Windows のディレクトリを WSL からアクセス:
git config --global --add safe.directory /mnt/c/Users/you/repo
Q8. IDE / GUI で発生
- VS Code: 「フォルダを開く」で正しい repo を選ぶ
- SourceTree: リポジトリを追加
- GitKraken: Open a Repo
Q9. Homebrew でもエラー
brew update
# fatal: not a git repository (Homebrew内で)
対処:
cd $(brew --repository)
git status
# → Homebrew 内の Git repo を修復
詳細はfatal: bad revision ‘HEAD’ の記事も参照。
Q10. ~/.git を誤って作った
cd ~
git init
# ホームディレクトリ全体が Git 管理下に!
# 修正
rm -rf ~/.git
ホームで git init しないは鉄則。
Q11. git rev-parse で確認
git rev-parse --is-inside-work-tree
# true → 内部
# エラー → 外部
最も確実な診断。
Q12. worktree の詳細
git worktree list # 一覧
git worktree add ... # 追加
git worktree remove ...# 削除
git worktree repair # 修復
参考リンク・関連資料
Git 公式
- git-init Documentation – init 公式
- git-worktree Documentation – worktree 公式
- gitrepository-layout – .git の構造
まとめ
fatal: not a git repository の解決、要点を再整理します。
5大原因
| # | 原因 | 診断 | 解決 |
|---|---|---|---|
| ① | git init 忘れ | .git なし | git init |
| ② | ディレクトリ違い | pwd で確認 | cd で移動 |
| ③ | .git 削除 | 誤って rm | 再 init or clone |
| ④ | Docker / WSL | volume 問題 | マウント設定 |
| ⑤ | worktree 壊れた | .git がファイルで壊れた | worktree repair |
最速の解決手順
# 1. 現在位置確認
pwd
# 2. .git 確認
ls -la | grep .git
# 3. なければ
git init # 新規
# または
cd /correct/path # 別の場所
# 4. 実行
git status
.git 探索の仕組み
現在のディレクトリ → 親 → 親 → ... → / まで遡って探索
どこにもなければエラー
診断コマンド
pwd # 現在地
ls -la | grep .git # .git 存在
git rev-parse --is-inside-work-tree # Git 内か
git rev-parse --show-toplevel # Git のトップ
find . -name ".git" 2>/dev/null # .git 探索
env | grep GIT # 環境変数
予防策
git cloneから始める(init を避ける)pwdの習慣git statusを常用- Shell プロンプトに Git 情報
.gitを意識的に(削除注意)- Docker では volume 確認
- IDE のターミナルは project 内
事故防止
- ホームディレクトリで
git initしない .gitを手動で触らない- worktree は個別に管理
- submodule は init 忘れずに
- fresh clone は最強の復旧策
類似エラーとの見分け
fatal: not a git repository: そもそもGit repo外fatal: bad revision 'HEAD': HEAD の状態問題error: src refspec does not match any: push 時の初回問題fatal: refusing to merge unrelated histories: pull 時の共通祖先問題
これらの知識は、Git 初心者のオンボーディング・チーム開発・Docker / WSL 環境・CI/CD トラブル対応・Rails / Kamal 開発など、あらゆる場面で活用できます。本記事をブックマークしておけば、このエラーでGit を挫折することはなくなります。
本記事は2026年6月時点の情報をもとに、Git 2.40+ での動作確認・公式ドキュメントに基づき作成しています。Git のバージョンによって挙動が異なる場合があるため、最新の情報はGit公式ドキュメントもあわせてご確認ください。
-
前の記事
【完全ガイド】git fatal: bad revision ‘HEAD’ の原因と解決方法|empty repository・HEADファイル破損・実践対処まで徹底解説 2026.07.13
-
次の記事
【完全ガイド】git fatal: Unable to create ‘.git/index.lock’ の原因と解決方法|lock ファイル仕組み・IDE並行動作まで徹底解説 2026.07.14
コメントを書く