0
0

More than 1 year has passed since last update.

ステージしたものをないことにする

Posted at

ステージの内容をHEADに合わせる。

直前のコミット以降の修正を全て捨てる方法だ。
作業ツリーの内容が前の状態に上書きされる。

git reset 

  • HEADを履歴の中で移動させるコマンド
  • ステージや作業ツリーの内容をHEADに合わせる。

git reset --ファイル

オプション引数のファイル名を指定するとそのファイルだけHEADの状態に戻す。

git reset --hard

作業ツリーの内容もHEADの状態に戻す。

演習

hello.txtを修正してステージし、その後で、git reset --hardすると、ステージと作業ツリーが元の状態に戻ることを確認しよう。
git diffを使うといい。

気づき

この操作をすると、コミット前の修正を全て捨てることになるのでステージされる。

git diff

  • ファイルの間の差を表示する。
  • 作業ツリーとステージの差を出力する。

演習を解く

hello.txt
Hello!
I am a student.
hogehogehogehoge!!
hogehoge!!
**********@mbp training % git status
On branch master
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:   hello.txt

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

no changes added to commit (use "git add" and/or "git commit -a")
**********@mbp training % git diff
diff --git a/hello.txt b/hello.txt
index ****************
--- a/hello.txt
+++ b/hello.txt
@@ -1,4 +1,5 @@
 Hello!
 I am a student.
 hogehogehogehoge!!
+hogehoge!!
************@mbp training % git reset --hard
HEAD is now at a2f6d35 演習2.13.2のためにコミットする
***************@mbp training % git diff
***************@mbp training %
*************@mbp training % git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	.gitignore

nothing added to commit but untracked files present (use "git add" to track)
hello.txt
Hello!
I am a student.
hogehogehogehoge!!

ステージの内容が消えた。

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