BioErrorLog Tech Blog

試行錯誤の記録

git addを取り消す

先に結論:

# 全ファイルのgit addを取り消し
git reset

# 指定ファイルのgit addを取り消し
git reset <file>

はじめに

こんにちは、@bioerrorlogです。

git addを取り消したいケースがしばしばあります。

毎回ググり直しているので、備忘録を残します。

git addを取り消す

やり方

# 全ファイルのgit addを取り消し
git reset

# 指定ファイルのgit addを取り消し
git reset <file>


昔のGit(バージョン1.8.2以前)の場合、上記コマンドだとgit init直後の状態ではエラーになる場合があったようですが、今はもうgit init直後であっても上記コマンドが使えます。

"git reset" (without options or parameters) used to error out when you do not have any commits in your history, but it now gives you an empty index (to match non-existent commit you are not even on).

1.8.2.txt « RelNotes « Documentation - git.git - The core git plumbing

動作事例

※ 作業時のGitバージョン

$ git --version
git version 2.25.1

全ファイルのgit addを取り消し

git addされた2つのファイルをgit resetで取り消す例:

$ touch 1.txt 2.txt
$ git add 1.txt 2.txt
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    new file:   1.txt
    new file:   2.txt

$ git reset
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
    1.txt
    2.txt

nothing added to commit but untracked files present (use "git add" to track)

指定ファイルのgit addを取り消し

git addされた2つのファイルのうち、指定のファイルのみをgit resetで取り消す例:

$ touch 1.txt 2.txt
$ git add 1.txt 2.txt
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    new file:   1.txt
    new file:   2.txt

$ git reset 1.txt
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    new file:   2.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    1.txt

git init直後のgit addを取り消し

git init直後のgit addを、git resetで取り消す例:

$ git init
$ touch test.txt
$ git add test.txt
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
    new file:   test.txt

$ git reset
$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    test.txt

nothing added to commit but untracked files present (use "git add" to track)

更新したファイルのgit addを取り消し

ファイルの更新でも同じくgit resetが動作することの確認:

$ echo aaa >> 1.txt
$ git add 1.txt 
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   1.txt

$ git reset 1.txt
Unstaged changes after reset:
M   1.txt

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   1.txt

no changes added to commit (use "git add" and/or "git commit -a")

おわりに

以上、git addを取り消すやり方をまとめました。

ちょっとした備忘録でしたが、どなたかの参考になれば幸いです。

[関連記事]

www.bioerrorlog.work

www.bioerrorlog.work

www.bioerrorlog.work

参考

How do I undo 'git add' before commit? - Stack Overflow

Git - git-reset Documentation

1.8.2.txt « RelNotes « Documentation - git.git - The core git plumbing