💬

Gitの変更を取り消すには( git statusした時の modifiedの取り消し方やコミットの取り消し方など)

に公開

この記事の背景

謝って作業をしてしまって大量にファイルが変更されてしまいました。
そんな時にGitのあるコマンドを使えば解決できました。

どんな状況だった

まだgit addする前です。こんなに不要な変更があるにもかかわらず、
一瞬で可決できました。

taro% git status
On branch development
Your branch is up to date with 'origin/development'.

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:   .env.development.example
        modified:   Gemfile
        modified:   app/〇〇controller.rb
        modified:   app/〇〇controller.rb
        modified:   app/〇〇controller.rb
        modified:   app/〇〇controller.rb
        modified:   app/〇〇controller.rb
        modified:   app/〇〇controller.rb
        modified:   app/〇〇controller.rb
        modified:   app/〇〇controller.rb
        modified:   app/〇〇controller.rb
        modified:   app/〇〇controller.rb
        modified:   app/〇〇controller.rb
        modified:   config/routes/web.rb
        modified:   db/migrate/20230808023747_create_users.rb
        modified:   lib/〇〇/jwt.rb
        modified:   lib〇〇.rb
        modified:   spec/rails_helper.rb
        modified:   spec/spec_helper.rb
        modified:   test/〇〇test.rb
        modified:   test/〇〇test.rb
        modified:   test/〇〇test.rb
        modified:   test/〇〇test.rb

変更を破棄して最後のコミットの状態に戻す

このコマンドを使えばmodified:は全部取り消されました

 git checkout -- .

全てを元に戻す

上のコマンドと同じかもしれないですが、このコマンドも使えるみたいです。

git checkout .

特定のファイルの変更を取り消す

他にも調べたら以下のようなやり方も出てきました。

git checkout <ファイル名>

特定のディレクトリ以下の変更を再起的に取り消す

git checkout <ディレクトリ名>

変更をステージングエリアから取り消す

git addをした時の取り消し方です。

git reset 

これでgit add .などでステージングエリアに入ったファイルは全部取り消せます。

ファイル名を指定すると指定したファイルを取り消せます。

git reset ファイル名

今回はGemfileです。

git reset Gemfile

実際の画像で確認してみます。

スクリーンショット 2023-11-16 12.51.03.png

赤いのはステージングエリアから消したファイル(git addする前の状態)で
緑の方はステージングエリアにある状態(ait addされたファイル)です。

コミットの取り消し方

⚫︎直近のcommitの変更点を確認する

$ git diff HEAD~1

⚫︎直近のcommitを取り消す

$ git reset --soft HEAD^

ファイルを消してしまった場合

ファイルがいくつか削除されて、この変更がまだステージングに追加されていない、つまりコミットもされていない
ので直前にコミットした状態まで簡単に戻すことができるコマンドがrestoreコマンドです。

全部消してしまった場合は

git restore .

です。

git restore ファイル名

未追跡ファイルや変更が取り消し

これらのコマンドを実行することで、未追跡ファイルや変更が取り消され、ワーキングディレクトリがクリーンな状態に戻ります。

# 未追跡ファイルを取り消す
git clean -fd

# ステージングエリアの変更を取り消す
git reset

# ワーキングディレクトリの変更を取り消す
git checkout -- 

未追跡とは

% git status
On branch development
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        node_modules/
        package.json
        vendor/bundle/ruby/3.1.0/
        yarn.lock

みたいなファイルです。今のブランチで作ったファイルです。

ファイルごとに変更を取り消す

ファイルごとに変更を取り消すやり方です。

git restore --source=HEAD --staged --worktree <ファイル名>

例えばGemfileを取り消したい場合は以下のコマンドです。

git restore --source=HEAD --staged --worktree Gemfile

こうしてGemfileは消されました。

このコマンドは、指定されたファイルをステージングエリアとワーキングディレクトリの両方から元の状態に戻します。

注意

変更を取り消す前に、変更内容を確認してください。
また、コミットしていない変更がある場合は注意してください。コミットしたくない変更がある場合は、それらをステージングエリアから取り消してから上記の手順でやってみてください。

資料

https://www.yoheim.net/blog.php?q=20140201#google_vignette

https://railstutorial.jp/chapters/beginning?version=7.0#sec-the_hello_application

Discussion