4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

5分で読んで実行できる!git resetでadd,commitを取り消す方法

Last updated at Posted at 2021-03-31

はじめに

半分自分のメモ用としてこの記事を書きました。
間違えたファイルをaddしてしまったり、コミットメッセージに誤りがあるのにcommitしてしまった場合などに有効です。

目次

addを取り消す方法

主に以下の三つを用途に応じて使い分ける。

ステージングエリアからすべてのファイルを削除(ワークツリーに戻す)

git reset 

特定のファイルのみステージングエリアから削除(ワークツリーに戻す)

git reset [ファイル名] 

特定のファイルのみステージングエリアから削除し、ワークツリーにも戻さない

git checkout HEAD [ファイル名]

commitを取り消す方法

まず、以下のように、git logで戻りたいcommitまでのコミットIDを調べてください。

user:~$ git log
commit 6933d5392d85476afd641dead45878f036fc82b6 (HEAD -> each-break-nested)
Author: ******* <******@gmail.com>
Date:   Thu Apr 1 07:35:34 2021 +0900

   aaaソースコードの追加(間違えてcommitしたファイル)

commit b32d8fa20dfb48b421015784840271a49c013a15 (origin/each-break-nested)
Author: ******* <******@gmail.com>
Date:   Wed Mar 31 11:24:33 2021 +0900

   aaaを利用したソースコードのソースコードの追加(戻りたいcommit)

今回の例でいうと、戻りたいcommitまでのコミットIDは
<b32d8fa20dfb48b421015784840271a49c013a15>です!

commitのみ取り消し

git reset --soft [コミットID]

commit、addの取り消し

git reset --mixed [コミットID]

commit、add、ワーキングツリーでの変更の取り消し

git reset --hard [コミットID]

参考記事

4
2
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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?