0
0

More than 1 year has passed since last update.

【Git】間違えてadd,commitしたときの対処方法

Last updated at Posted at 2021-12-07

はじめに

「コミットに含めたくないファイルまでステージングしてしまった!」、「前のコミットに現在修正中のものも加えたい!」
「リファクタリング中にあれこれ試していたら機能が動かなくなってしまった。一旦、ファイル変更前の状態に戻したい。」
等など、たまにうっかりやってしまうGitミスを解決する方法をまとめました:sunny:

add(ステージ)を取り消す。

//sample.htmlのステージを取り消す。(ファイル名、ディレクトリ名の指定が可能)
git reset HEAD -- sample.html
//全変更を取り消す。
git reset HEAD -- .

直前のコミットを取り消しする。

//ローカルのファイルを残す
git reset --soft HEAD^
//ローカルのファイルも消す
git reset --hard HEAD^

直前のコミットを修正する。(現在の変更をaddして追加することも可能。)

エディタが立ち上がるので、コミットメッセージを修正して「esc」→「q」→「w」
(-mでコメントを修正してもOK!)

//ファイル名、ディレクトリ名の指定が可能
git commit --amend

ファイル変更の取り消し

//sample.htmlへの変更を取り消す。(ファイル名、ディレクトリ名の指定が可能)
git checkout -- sample.html
//全変更を取り消す。
git checkout -- .

addの削除、コミットの修正等を行ったら、
「git status」、「git log --oneline」などで確認しましょう!

0
0
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
0
0