0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Gitでファイルの変更やaddを取り消す方法

Posted at

はじめに

いざというときのために作業を取り消すコマンドを知っておくと安心です。同じような意味のコマンドもあるため、どれを使えばいいのか迷ってしまわないよう最初におすすめのコマンドを記載しています。見出し横の「A→B」という表記はBをAの状態に戻すという意味です。

ファイルの変更を取り消す(インデックス→ワークツリー)

まだコミットしていないワークツリー内のファイルの変更を取り消すには以下を使用。

git restore ファイル

以下もすべて同じ動作です。

git restore -- ファイル # ファイル名が"--"で始まる場合などに
git restore --worktree ファイル
git restore --W ファイル
git checkout -- ファイル # gitのバージョンが古くrestoreが使えない場合に
git checkout ファイル # 同名ブランチが存在しない場合のみ、ファイル復元として動作

補足:ファイルを復元するgit restore(とブランチを切り替えるgit switch)はバージョン2.23(2019年8月)でgit checkoutの役割を分離するために導入されました。バージョンが古い場合はgit checkoutを使用してください。

addを取り消す(HEAD→インデックス)

git restore --staged ファイル

以下もすべて同じ動作です。

git restore -S # Sは大文字
git reset -- ファイル # gitのバージョンが古くrestoreが使えない場合に
git reset ファイル # ファイル名がheadやコミットのハッシュではない場合に
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?