resetとcheckout その1

resetとcheckoutはとても混同しやすい。なんとなくおさらいをしてみる。

resetとcheckout

resetはステージしているファイルをステージから降ろすコマンド。
checkoutはワークツリーの変更をHEADの状態に戻すコマンド。

reset

これから新規作成するtop.htmlに対してresetコマンドを叩き込んでみる。

$ vim top.html

top.htmlを新規作成した。

statusコマンドでリポジトリの状態を確認をする。

$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	top.html
nothing added to commit but untracked files present (use "git add" to track)

コミットしていないtop.htmlが表示されている。

このtop.htmlをステージングしてみる。

git add top.html

ステージングした。

statusコマンドでステージにあがったか確認をする。

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	new file:   top.html
#

ステージングしているのが確認できた。

resetコマンドを実行する。

$ git reset top.html

statusコマンドでステージから降ろされているか確認をする。

$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	top.html
nothing added to commit but untracked files present (use "git add" to track)

"Untracked files"にtop.htmlがある。resetされている。

checkout

index.htmlというファイルにcheckoutコマンドをぶち込んでみる。

リポジトリに以下の内容のindex.htmlというファイルがあるとする。

<html>
  <head></head>
  <body></body>
</html>

このファイルに変更を加え、以下のようにした。

<html>
  <head></head>
  <body>
    <h1>おろし大根</h1>
  </body>
</html>

diffコマンドを実行すると以下のようになる。

$ git diff
diff --git a/index.html b/index.html
index a8d6245..a2f4c7d 100644
--- a/index.html
+++ b/index.html
@@ -1,2 +1,6 @@
-<html></html>
-
+<html>
+  <head></head>
+  <body>
+    <h1>おろし大根</h1>
+  </body>
+</html>

statusコマンドでリポジトリの状態を見てみる。

$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#	modified:   index.html
#
no changes added to commit (use "git add" and/or "git commit -a")

index.htmlに変更があると書いている。

ここでcheckoutコマンドを実行してみる。

$ git checkout index.html

statusコマンドでリポジトリの状態を確認してみる。

$ git status
# On branch master
nothing to commit (working directory clean)

変更が消えている!

$ vim index.html
<html></html>

変更が消えている!

変更を取り消すという目的が同じなので並べてみると余計に紛らわしく感じる。
次はオプションつけたのを書く。