MeWrite Docs

fatal: not a git repository

Gitリポジトリとして初期化されていないディレクトリでGitコマンドを実行した場合に発生するエラー

概要

fatal: not a git repository (or any of the parent directories): .git は、Gitリポジトリとして初期化されていないディレクトリでGitコマンドを実行した場合に発生するエラーです。

エラーメッセージ

fatal: not a git repository (or any of the parent directories): .git
fatal: not a git repository (or any parent up to mount point /)

原因

  1. リポジトリ未初期化: git init が実行されていない
  2. 間違ったディレクトリ: リポジトリ外でコマンドを実行
  3. .git ディレクトリの削除: 誤って削除された
  4. サブモジュールの問題: サブモジュールが正しく初期化されていない
  5. 権限の問題: .git ディレクトリにアクセスできない

解決策

1. リポジトリを初期化

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# 新しいリポジトリを初期化
cd /path/to/project
git init

# 初期化されたか確認
ls -la .git

# 最初のコミット
git add .
git commit -m "Initial commit"

2. 現在のディレクトリを確認

1
2
3
4
5
6
7
8
# 現在のディレクトリ
pwd

# Git ルートディレクトリを確認
git rev-parse --show-toplevel

# .git があるか確認
ls -la | grep .git

3. リモートリポジトリからクローン

1
2
3
4
5
6
7
8
# HTTPS でクローン
git clone https://github.com/username/repository.git

# SSH でクローン
git clone git@github.com:username/repository.git

# クローンしたディレクトリに移動
cd repository

4. .git ディレクトリの復元

1
2
3
4
5
6
7
8
9
# リモートリポジトリがある場合
# 既存のファイルを退避
mv /path/to/project /path/to/project_backup

# 再クローン
git clone https://github.com/username/repository.git project

# 必要に応じてファイルをコピー
cp -r /path/to/project_backup/* /path/to/project/

5. サブモジュールの初期化

1
2
3
4
5
6
7
8
9
# サブモジュールを初期化
git submodule init
git submodule update

# または一括で
git submodule update --init --recursive

# サブモジュールの状態を確認
git submodule status

6. GIT_DIR 環境変数の確認

1
2
3
4
5
6
7
8
# GIT_DIR が設定されているか確認
echo $GIT_DIR

# 設定されている場合はクリア
unset GIT_DIR

# または正しいパスを設定
export GIT_DIR=/path/to/project/.git

7. 権限の問題を解決

1
2
3
4
5
6
# .git ディレクトリの権限を確認
ls -la .git

# 権限を修正
sudo chown -R $(whoami) .git
chmod -R 755 .git

8. bare リポジトリの問題

1
2
3
4
5
6
# bare リポジトリ(作業ディレクトリなし)の場合
# --bare オプションでクローン
git clone --bare https://github.com/username/repository.git

# 通常のリポジトリに変換
git clone repository.git new-repository

9. worktree の問題

1
2
3
4
5
6
7
# worktree を使用している場合
git worktree list

# worktree ディレクトリ内でコマンドを実行していないか確認
# worktree には .git ファイル(ディレクトリではない)がある
cat .git
# gitdir: /path/to/main/.git/worktrees/branch-name

特殊なケース

CI/CD 環境

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# GitHub Actions
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4  # リポジトリをチェックアウト
      - run: git status  # これでエラーにならない

# shallow clone の場合
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0  # 全履歴を取得

Docker 環境

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Dockerfile
FROM node:18

WORKDIR /app

# .git も含めてコピー
COPY . .

# または git clone
RUN git clone https://github.com/username/repo.git .
1
2
3
4
5
# .dockerignore で .git を除外している場合
cat .dockerignore
# .git  # これがあると .git がコピーされない

# コンテナ内で git を使う場合は除外しない

VS Code Remote

1
2
3
4
5
6
7
8
# Remote - SSH や Remote - Containers で
# リポジトリが認識されない場合

# リポジトリのルートでターミナルを開く
code /path/to/repository

# Git パスを確認
git config --get core.worktree

デバッグのコツ

リポジトリ情報の確認

1
2
3
4
5
6
7
8
# Git の設定を確認
git config --list

# リモートを確認
git remote -v

# ブランチを確認
git branch -a

ディレクトリ構造の確認

1
2
3
4
5
6
7
8
# .git ディレクトリの中身
ls -la .git/

# HEAD ファイルの内容
cat .git/HEAD

# config ファイル
cat .git/config

安全なディレクトリの設定

1
2
3
4
5
6
7
# Git 2.35.2 以降で必要な場合
# "fatal: unsafe repository" エラーの場合

git config --global --add safe.directory /path/to/repository

# または全リポジトリを許可(注意して使用)
git config --global --add safe.directory '*'

Git の他のエラー

最終更新: 2025-12-08