git add . で、入れるつもりのないファイルまで add してしまった。add は「次のコミットに入れる物の箱に入れる」操作なので、箱から出すだけなら、書いた中身は消えません。 1つだけ外すなら、次の1行です。
まずこれを打つ
git restore --staged memo.txtmemo.txt の所に外したいファイル名を書きます。fatal: could not resolve HEAD と出たら、まだ1回もコミットしていないリポジトリです。下の表の3行目へ。
add した後の画面を見る
Git には、コミットに入れる物を一時的に置いておく場所があり、ステージ(staging area、インデックスとも言います)と呼びます。git add はファイルをステージに載せる操作で、取り消しは「ステージから降ろす」ことです。
下は、index.html と style.css を直したついでに、メモの memo.txt まで add してしまった画面です。git status の中に、Git が取り消し方を書いてくれています。
hanako@MacBook-Air cafe % git add . hanako@MacBook-Air cafe % git status On branch main Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: index.html new file: memo.txt modified: style.css
(use "git restore --staged <file>..." to unstage) は「ステージから降ろすには、これを打ってね」という案内です。困ったら、まず git status の案内を読むのが近道です。
何を戻したいかで、打つものが変わる
| 場面 | 見分け方 | 打つもの |
|---|---|---|
| 1つだけ外したい | status に restore --staged の案内 | git restore --staged ファイル名 |
| 全部外したい | 同じ | git restore --staged . |
| まだ1回もコミットしていない | No commits yet と出る | git rm --cached ファイル名 |
| 変更そのものも捨てたい | 書いた中身が要らない | 降ろしてから git restore ファイル名(中身が消える。新しく作ったファイルには効かない) |
1つだけ外す・全部外す
ファイル名を書けば1つだけ、.(いまのフォルダ以下の全部)を書けば全部を降ろします。下の git status --short は、状態を1行ずつ短く出す書き方です。左の列の M はステージに載っている変更、右の列の M は載っていない変更、?? は Git がまだ管理していない新しいファイルを表します。
hanako@MacBook-Air cafe % git restore --staged memo.txt hanako@MacBook-Air cafe % git status --short M index.html M style.css ?? memo.txt hanako@MacBook-Air cafe % git restore --staged . hanako@MacBook-Air cafe % git status --short M index.html M style.css ?? memo.txt
どちらも、ファイルの中身には触っていません。もう一度 git add すれば、また載せられます。
まだ1回もコミットしていない時は git rm —cached
git init した直後で、まだ1回もコミットしていないリポジトリでは、git status の案内が変わります。この時に git restore --staged を打つと、戻す先のコミットが無いので止まります。
hanako@MacBook-Air cafe % git status On branch main No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: index.html new file: memo.txt hanako@MacBook-Air cafe % git restore --staged memo.txt fatal: could not resolve HEAD hanako@MacBook-Air cafe % git rm --cached memo.txt rm 'memo.txt' hanako@MacBook-Air cafe % git status --short A index.html ?? memo.txt
rm 'memo.txt' と出ますが、消えたのはステージの上の memo.txt だけで、フォルダの中のファイルは残っています。--cached が「ステージからだけ外す」という指定です。全部を降ろしたい時は、git rm --cached -r . のように -r を付けます。
git reset との関係
ネットの記事で git reset ファイル名 や git reset HEAD ファイル名 を見かけることがあります。ファイル名を付けた git reset も、ステージから降ろすだけで、中身には触りません。git restore は Git 2.23 で「戻す」専用の命令として加わったもので、それより前は git status が git reset HEAD を案内していました。コミットが1つ以上あれば、どちらでも同じ結果になります(git reset HEAD ファイル名 は、まだコミットが無いリポジトリでは unknown revision と出て止まります)。手元で試すと、ファイル名なしの git reset は、まだ1回もコミットしていないリポジトリでも全部を降ろせました。
ただし、git reset に --hard を付けると、書いた中身まで捨てる命令に変わります。降ろすだけなら --hard は要りません。
hanako@MacBook-Air cafe % cat index.html <h1>cafe</h1> <p>menu</p> hanako@MacBook-Air cafe % git restore --staged index.html hanako@MacBook-Air cafe % git restore index.html hanako@MacBook-Air cafe % cat index.html <h1>cafe</h1>
本当に捨てていい変更の時だけ使います。なお、一度もコミットしていない新しいファイルに打つと、did not match any file(s) known to git と出て何も起きませんでした。新しいファイルが要らない時は、ファイルそのものを消します。もう1つの勘違いは、コミットが1つ以上あるリポジトリで、案内を覚えたまま git rm --cached を打つことです。手元で試すと、すでにコミットに入っている style.css に打った時、git status --short が D style.css(削除をステージに載せた)と ?? style.css の2行になりました。そのままコミットすると、次のコミットから style.css が外れます。コミットが1つ以上ある時は git restore --staged を使います。
git status案内が restore --staged か rm --cached かを見る- 1つだけ →
git restore --staged ファイル名全部なら. - コミット0なら
git rm --cached ファイル名ファイルはフォルダに残る git status --shortM が右の列に移っていれば降りている
ステージとコミットの関係は、図解の Git 入門 で絵にしています。コミットまでしてしまった後なら git commit を取り消したい(push前) を見てください。
この記事の画面は、macOS 26.6 / 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)