Git で「fatal: not a git repository」が解決できない
カレントディレクトリにもその親にも .git が無いために出るエラー。
新規なら git init、既存リポジトリなら該当ディレクトリの中へ cd する。
公開: 更新:
要約
fatal: not a git repository (or any of the parent directories): .git は、いま実行しているディレクトリにも、その親をたどっても .git が見つからないときに出る。
Git は「今いる場所」から上へ親ディレクトリを探索して .git を見つける仕組みなので、リポジトリの外にいるか、そもそも初期化していないのが原因になる。
新規プロジェクトなら git init、既存リポジトリなら該当ディレクトリの中へ移動すれば解消する。
実行例
リポジトリ外では git status も git rev-parse --show-toplevel も同じ fatal を返し、どちらも終了コードは 128 になる。git init を通したあとは rev-parse が作業ツリーのパスを返し、同じ git status が終了コード 0 で通る。
$ git status
fatal: not a git repository (or any of the parent directories): .git
$ echo $?
128$ git rev-parse --show-toplevel
fatal: not a git repository (or any of the parent directories): .git
$ echo $?
128$ git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: will change to "main" in Git 3.0. To configure the initial branch name
hint: to use in all of your new repositories, which will suppress this warning,
hint: call:
hint:
hint: git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: git branch -m <name>
hint:
hint: Disable this message with "git config set advice.defaultBranchName false"
Initialized empty Git repository in /tmp/tmp.hhKpdg/.git/
$ echo $?
0$ git rev-parse --show-toplevel
/tmp/tmp.hhKpdg$ git status --short
$ echo $?
0— 2026-09-01 時点の出力
検証環境
- 検証日
- 実行環境
alpine/gitAlpine Linux v3.24- バージョン
- Git 2.54.0
この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。
よくある原因
- リポジトリ外での実行: ホームディレクトリやデスクトップなど、
.gitを持たない場所でコマンドを打っている。 - 未初期化:
git initもgit cloneもまだ実行していない。 .gitの削除: 隠しフォルダを整理する際などに.gitを消してしまった。- clone フォルダの外: clone は成功したが、生成されたサブフォルダへ入らず親で実行している。
解決策
1. 今どこにいるか確認する
pwd
git rev-parse --show-toplevelgit rev-parse --show-toplevel はリポジトリ内ならルートの絶対パスを返し、外なら同じ fatal を返す。
これで「中にいるか外にいるか」を切り分けられる。
2. 新規リポジトリを作る
git initカレントディレクトリに .git が作られ、以降の git コマンドが通るようになる。
3. 既存リポジトリの中へ移動する
cd path/to/repo
git statusclone 済みなら、生成されたフォルダ名へ cd してから操作する。
4. .git を消していないか確認する
ls -a.git が見当たらなければ履歴ごと失われている。
リモートがあるなら git clone <url> で取り直すのが速い。