Gitに入門する -その5-

blog.takanabe.tokyo

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

「ひとりでつかう - ブランチを知る」より。


※ブランチとはコミットオブジェクトを指すもの。

リポジトリにあるブランチを確認する

$ git branch
* master

masterというブランチはリポジトリを作った時に作られるブランチ。

魔法を放つ。

$ git config --global alias.graph "log --graph --date-order --all --pretty=format:'%h %Cred%d %Cgreen%ad %Cblue%cn %Creset%s' --date=short"

そうするとgit graphをしたときにキレイにフォーマットされたものが見れる。

$ git graph
* bc4c6ca  (HEAD -> master) 2015-12-01 user_name ファイル名をhoge.txtからhogehoge.txtに変更
* ea41e3c  2015-11-28 user_name piyo.txtも削除
* 678dd05  2015-11-28 user_name fuga.txtを削除
* 42201c7  2015-11-28 user_name fuga.txtとpiyo.txtを追加
* 3d3e14a  2015-11-28 user_name hogeをfugaに変更しました
* 03e378e  2015-11-28 user_name hogeをfugaしました。

HEADとは現在のブランチがどれか、を教えてくれるもの。

②ブランチを作成する。

$ git branch my_first_branch

$ git branch
* master
  my_first_branch

$ git graph
* bc4c6ca  (HEAD -> master, my_first_branch) 2015-12-01 user_name ファイル名をhoge.txtからhogehoge.txtに変更
* ea41e3c  2015-11-28 user_name piyo.txtも削除
* 678dd05  2015-11-28 user_name fuga.txtを削除
* 42201c7  2015-11-28 user_name fuga.txtとpiyo.txtを追加
* 3d3e14a  2015-11-28 user_name hogeをfugaに変更しました
* 03e378e  2015-11-28 user_name hogeをfugaしました。

なにかコミットしてみると↓

$ git graph
* b3cd5a3  (HEAD -> master) 2015-12-04 user_name hogehoge.txtの中身をpiyopiyoに変更。
* bc4c6ca  (my_first_branch) 2015-12-01 user_name ファイル名をhoge.txtからhogehoge.txtに変更
* ea41e3c  2015-11-28 user_name piyo.txtも削除
* 678dd05  2015-11-28 user_name fuga.txtを削除
* 42201c7  2015-11-28 user_name fuga.txtとpiyo.txtを追加
* 3d3e14a  2015-11-28 user_name hogeをfugaに変更しました
* 03e378e  2015-11-28 user_name hogeをfugaしました。

my_first_branchがさっきのままでmasterブランチだけが新しいコミットを示した。

③ブランチを切り替える

$ git checkout my_first_branch
Switched to branch 'my_first_branch'
$ git graph
* b3cd5a3  (master) 2015-12-04 user_name hogehoge.txtの中身をpiyopiyoに変更。
* bc4c6ca  (HEAD -> my_first_branch) 2015-12-01 user_name ファイル名をhoge.txtからhogehoge.txtに変更
* ea41e3c  2015-11-28 user_name piyo.txtも削除
* 678dd05  2015-11-28 user_name fuga.txtを削除
* 42201c7  2015-11-28 user_name fuga.txtとpiyo.txtを追加
* 3d3e14a  2015-11-28 user_name hogeをfugaに変更しました
* 03e378e  2015-11-28 user_name hogeをfugaしました。

HEADがmy_first_branchを指すようになった。
ブランチをcheckoutすると、HEADが切り替わり、
作業ディレクトリの内容がそのコミットオブジェクトの状態に変更される。

my_first_branchのnyan.txtの中身をnyannyanに変更してコミット。

$ git graph
* 4e4324f  (HEAD -> my_first_branch) 2015-12-04 user_name nyan.txtのnyanをnyannyanに変更
| * b3cd5a3  (master) 2015-12-04 user_name hogehoge.txtの中身をpiyopiyoに変更。
|/
* bc4c6ca  2015-12-01 user_name ファイル名をhoge.txtからhogehoge.txtに変更
* ea41e3c  2015-11-28 user_name piyo.txtも削除
* 678dd05  2015-11-28 user_name fuga.txtを削除
* 42201c7  2015-11-28 user_name fuga.txtとpiyo.txtを追加
* 3d3e14a  2015-11-28 user_name hogeをfugaに変更しました
* 03e378e  2015-11-28 user_name hogeをfugaしました。


[* bc4c6ca 2015-12-01 user_name ファイル名をhoge.txtからhogehoge.txtに変更]
このコミットを起点にして、
異なる2つのコミットが生まれて枝分かれした。