git status や git add を打ったら、fatal: not a git repository (or any of the parent directories): .git と出た。これは「いまいるフォルダにも、その上のフォルダにも、Git の記録(.git というフォルダ)が見つからない」という意味です。多くは、ターミナルが思っていたのと違う場所にいます。まず、いる場所と中身を見ます。
まずこれを打つ
pwd
ls -apwd はいる場所、ls -a は隠しファイルも含めた中身です。一覧にプロジェクトのフォルダ名が出たら cd でその中へ。自分のファイルが並んでいるのに .git が無ければ、まだ Git を始めていません(下の表)。
何が起きているか
Git は、コマンドを打った場所から1つずつ上のフォルダへさかのぼって、.git を探します。.git は git init か git clone をした時に作られるフォルダで、コミットの記録が入っています。どこにも見つからないと、このエラーで止まります。
下は、デスクトップにある cafe というプロジェクトの、1つ外(デスクトップ)で git status を打った画面です。
hanako@MacBook-Air Desktop % pwd /Users/hanako/Desktop hanako@MacBook-Air Desktop % git status fatal: not a git repository (or any of the parent directories): .git hanako@MacBook-Air Desktop % ls cafe hanako@MacBook-Air Desktop % cd cafe hanako@MacBook-Air cafe % ls -a . .. .git index.html hanako@MacBook-Air cafe % git status On branch main nothing to commit, working tree clean
(ls -a の一覧は、再現した環境では1行に1つずつ出ました。ターミナルのウインドウでは、横に並んで出ることがあります。. と .. は「いまのフォルダ」と「1つ上」の印です。)
Git は上へはさかのぼりますが、下のフォルダの中までは探しません。だから、cafe の中や、その下の cafe/css で打てば動き、1つ外のデスクトップで打つと止まります。
原因の見分け方
pwd と ls -a で見えたもの | 原因 | 打つもの |
|---|---|---|
| 一覧にプロジェクトのフォルダ名がある | 1つ上にいる(git clone した直後に多い) | cd フォルダ名 |
pwd の場所が、思っていたフォルダと違う | 別のフォルダにいる | cd で移る。VS Code ならフォルダを開き直す |
自分のファイルはあるが、.git が無い(初めて Git を使う) | まだ git init も git clone もしていない | 新しく始めるなら git init、GitHub にあるなら git clone |
前は動いていたのに、.git だけが無い | .git を消してしまった | push 済みなら別の場所に clone し直す |
1つ上にいる:clone の直後
git clone は、打った場所の中に、新しいフォルダを1つ作ります。clone したあとに cd をせずに git を打つと、作られたフォルダの外にいるので止まります(下は、手元のフォルダから clone して再現した画面です)。
hanako@MacBook-Air work % git clone ../cafe menu-site Cloning into 'menu-site'... done. hanako@MacBook-Air work % git status fatal: not a git repository (or any of the parent directories): .git hanako@MacBook-Air work % cd menu-site hanako@MacBook-Air menu-site % git status On branch main Your branch is up to date with 'origin/main'. nothing to commit, working tree clean
別のフォルダにいる:VS Code も確かめる
ターミナルの新しいウインドウは、ふつうホームフォルダ(~)から始まります。プロジェクトで作業するたびに cd で移る必要があります。Finder のフォルダをターミナルのウインドウにドラッグすると、その場所が文字で入るので、cd と打ってからドラッグすると移れます。
VS Code の中のターミナルは、ふつう VS Code で開いたフォルダから始まります。プロジェクトの1つ上のフォルダ(デスクトップなど)を開いていると、同じエラーになります。「ファイル」→「フォルダーを開く」で、.git があるフォルダそのものを開き直します。
まだ Git を始めていない
自分のファイルはあるのに .git が無いなら、そのフォルダはまだ Git の記録を始めていません。プロジェクトのフォルダに cd で入ってから、git init を打ちます。
hanako@MacBook-Air cafe % git status fatal: not a git repository (or any of the parent directories): .git hanako@MacBook-Air cafe % git init Initialized empty Git repository in /Users/hanako/Desktop/cafe/.git/
init.defaultBranch を設定していない Git では、Initialized の前に hint: で始まる行が何行も出て、枝の名前が master になります。エラーではありません(この記事の画面は main に設定した環境です)。
Initialized … in の後ろに出る場所が、記録を始めたフォルダです。ここがプロジェクトのフォルダになっているかを、目で見て確かめます。GitHub にすでにあるプロジェクトなら、git init ではなく git clone で取ってきます。
.git を消してしまった
.git は隠しフォルダなので、ls -a で見ても無ければ、消えています。下は、練習用のフォルダで .git を rm -rf で消したあとの画面です。ファイルは残っていますが、Git の記録が無いので、git log も止まりました。
hanako@MacBook-Air menu % rm -rf .git hanako@MacBook-Air menu % ls -a . .. a.txt hanako@MacBook-Air menu % git log fatal: not a git repository (or any of the parent directories): .git
コミットの記録は .git の中にしか無いので、git init をし直しても、昔の記録は戻りません(新しい空の記録が始まるだけです)。GitHub などに push していたなら、別のフォルダに git clone し直し、手元で変えていたファイルをそこへ写すのが確かです。rm で消したものは、Finder のゴミ箱には入りません。
pwdとls -aいる場所と、.gitがあるかを見る- 一覧にフォルダ名があれば
cd フォルダ名clone の直後はこれが多い .gitが無ければgit initかgit clone打つ前に、いる場所がプロジェクトのフォルダか確かめるgit statusOn branchと出れば直っている
pwd・ls・cd でフォルダを移る基本は、図解の ターミナル入門 で、git init で始まる記録(リポジトリ)の仕組みは Git 入門 で説明しています。git init したフォルダを初めて GitHub に送る時は、git push できない(初回) も参考になります。
この記事の画面は、macOS 26.6.2 / zsh 5.9 / git 2.51.0(一時フォルダに作った練習用のホームで再現) で実際に出したものです。
Gitで、ほかによくあるつまずき
git branch -d で error: the branch 'x' is not fully merged と出て消せないbranch -d で not fully mergedgit switch で fatal: invalid reference: <name> と出て枝に移れないgit switch で invalid reference消してしまったファイルを、git で元に戻したいgit で削除したファイルを戻すremote: Repository not found. / fatal: repository '...' not foundgit clone で Repository not foundgit merge で Already up to date. と出て、取り込みたい変更が入らないmerge で Already up to date.[email protected]: Permission denied (publickey).Permission denied (publickey)