LoginSignup
3
3

More than 5 years have passed since last update.

誤ってgit commitしたものをgit resetで取消方法

Posted at

適用場合:git commit したけど git push をしていない。

$ git status
  (use "git reset HEAD <file>..." to unstage)

    modified:   filea.txt

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

    fileb.txt

filea は修正したファイルで、 fileb は新規ファイルです。

filea のみを commit したいけど、あやまって:

$ git add .
$ git commit -m "msg"

で2つのファイルを全部 commit した。

新規ファイル fileb を取り戻したいなら、下記に手順で行います:

一つ前の commit 履歴に戻ります:

$ git reset --soft HEAD^

これでは git add . の状態に戻り、 git status で確認できます:

$ git status
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   fileb.txt
    modified:   filea.txt

提示の通り、 git reset HEADfilebstaging 領域から外します。

$ git reset HEAD fileb.txt

$ git status
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

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

    new file:   fileb.txt

再び git commit

この状態でも一度 commit すれば良いです:

$ git commit -m "msg"

もしすでにリモートレポジトリにPUSHしましたら、 git push -f は使えるのではないか。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3