Gitに入門する

blog.takanabe.tokyo

ステップ2の「Gitを初めからていねいに」をやる。

「Git とはなんぞや」でGitの概念を学んだ。
その後、
「ひとりでつかう - はじめてのコミット」で実際に手を動かして学ぶ。


①作業ディレクトリを作る

$ mkdir work_directory

②作業ディレクトリに移動してリポジトリを作成する

$ cd work_directory
$ git init

git initすることで、
「work_directory」という作業ディレクトリに対応するリポジトリが作成される。
ちなみに、リポジトリはwork_directoryの中にできた.gitがそれです。

③作業ディレクトリの中にhoge.txtを作って置いておく。

④git statusしてみる。

$ git status

すると、

略
# Untrachked files:~~~~
#
#  hoge.txt
略

と出てくる。

Untracked files、つまり、追跡してないファイル、
Gitが認識してないファイルということらしい。

⑤じゃあGitに認識してもらおう

$ git add hoge.txt

これでおk。
認識してくれたはず。

再度git statusしてみると、

#On branch master
#Changes to be committed:
#  (use "git rm --cached <file>..." to unstage)
#
#  new file:  hoge.txt

変更はコミットされます
unstageするには"git rm --cached ..."しろと。

⑥unstageする

$ git rm --cached hoge.txt

すると、

略
# Untrachked files:~~~~
#
#  hoge.txt
略

のようにhoge.txtがUntrachked filesに戻った。

⑦ステージングエリア

Gitは以下の3つがある。
・作業エリア
・ステージングエリア
リポジトリ

作業エリアにあるものをステージングエリアに移すのが、git addで、
ステージングエリアにあるものをリポジトリに上げるのがgit commitで、
ステージングエリアから作業エリアに戻すのが、先ほどのgit rm --cachedとなる。

⑧commitする!

$ git commit

すると・・・

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#       new file:   hoge.txt
#

変更に対してのメッセージを入れてくれと出たので、
変更点を記載して保存。
すると下記のようなメッセージが出て終了。

$ git commit
[master (root-commit) 03e378e] hogeをfugaしました。
 1 file changed, 1 insertion(+)
 create mode 100644 hoge.txt

⑨ログを見る

$ git log

commit 03e378e81f2df50494397ba0f6c61403ba145e5b
Author: sei-mei <sei-mei@gmail.com>
Date:   Sat Nov 28 10:00:00 2015 +0900

    hogeをfugaしました。