Git使いになろう:ひとりで使う(4:編集中のファイルを元に戻す)

ファイルを編集中にやっぱり「やーめた」となって、元に戻したいときに最新のコミット状態にしてみます。

~/sandbox/sample.txt の内容を

Ragtimeblues
https://ragtimeblues.net/
sample.txt

に変えました(最後の行を追加)。

状態を確認します。

$ 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:   sample.txt

no changes added to commit (use "git add" and/or "git commit -a")

となって、sample.txtが変更されていることがわかります。
この編集を破棄して、最新のコミット状態に戻すにはgit checkoutを使います。

$ git checkout HEAD sample.txt

sample.txtの中身を確認すると、

$ cat sample.txt 
Ragtimeblues
https://ragtimeblues.net/

となって、元に戻っています。状態を確認すると、

$ git status
On branch master
nothing to commit, working directory clean

で、何も変更がなくきれいであることがわかります。

Git使いになろう:ひとりで使う(3:二回目のコミット)

Gitリポジトリに登録したファイルを変更してみます。
~/sandbox/sample.txt の内容を

Ragtimeblues
https://ragtimeblues.net/

に変えました。

状態を確認してみます。

$ 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:   sample.txt

no changes added to commit (use "git add" and/or "git commit -a")

となり、「ファイルが変更されたけどコミット用にステージされていない(変更されたのはsample.txt)」という状態だということがわかります。「コミットされるものが更新されるように”git add … “を使え」とあるのでgit addを実行したいところですが一旦変更内容を確認してみます。

$ git --no-pager diff
diff --git a/sample.txt b/sample.txt
index e797928..1e85400 100644
--- a/sample.txt
+++ b/sample.txt
@@ -1 +1,2 @@
 Ragtimeblues
+https://ragtimeblues.net/

で、変更内容が確認できました 。では、git addして状態も確認してみます。

$ git add sample.txt 
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   sample.txt

変更がコミットされるものの中に
modified: sample.txt
がありますので、コミットします。コミットメッセージは

add URL

add the URL of the Ragtimeblues

とします。(2行目が空行なのは理由がありますが別の機会に説明します)

$ git commit
[master ee8831f] add URL
 1 file changed, 1 insertion(+)

とコミット完了です。
二回目のコミットが終ったところで、履歴を確認してみます。履歴の確認はgit logを使います。

$ git --no-pager log 
commit ee8831f07fcd5007816ef6e75682b826659a79cd
Author: tomohiro <tomohiro@example.com>
Date:   Fri Jan 1 20:51:25 2016 +0900

    add URL
    
    add the URL of the Ragtimeblues

commit ab374c5f64e3de33b079b9e013ac2cec7830935d
Author: tomohiro <tomohiro@example.com>
Date:   Fri Jan 1 16:57:09 2016 +0900

    first commit

で、履歴が確認できました。

Git使いになろう:ひとりで使う(2:初めてのコミット)

リポジトリを作ったので、ファイルを登録します。まずは登録したいファイルを作ります。
~/sandboxにsample.txtという名前のファイルを作ってみます。内容は

Ragtimeblues 

とします。

ここで作業している場所の状態を確認します。

$ git status
On branch master

Initial commit

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

	sample.txt

nothing added to commit but untracked files present (use "git add" to track)

と表示されました。「コミットされるものの中に含まれるように、”git add …”を使え」とあるので、やってみます。

$ git add sample.txt

そして、もう一度状態を確認します。

$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

	new file:   sample.txt

変更がコミットされるものの中に
new file: sample.txt
があります。((use “git rm –cached …” to unstage)とあるのはファイルを管理対象からはずすためです。)

それではコミットしてみます。git commitと入力するとデフォルトではviが立ち上がるので

first commit

と入力して、保存終了します。

$ git commit
[master (root-commit) ab374c5] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 sample.txt

と表示されて、コミットができました。

再度、状態を確認してみます。

$ git status
On branch master
nothing to commit, working directory clean

となり、コミットされていない変更はなくなりました。