branchとは
日本語で言えば枝。バージョンを枝分かれさせて管理します。
幹となるブランチはmasterです。
$ git branch
* master
git branchコマンドを実行するとmasterとでてきます。は現在のブランチを示しています。
branchを作ってみよう
branch-aをつくる
$ git checkout -b branch-a
Switched to a new branch 'branch-a'
$ git branch
* branch-a
master
git checkoutコマンドでブランチの作成、切り替えができます。
-bオプションで作成と切り替えが同時にできます。
以下の操作と同じになります。
$git branch branch-a
$git checkout branch-a
branchの確認
$ git branch
* branch-a
master
branch-aができて、現在のブランチがmasterからbranch-aに移っています。
branch-aでファイルの変更をしてコミット
$ vi read.me
$ cat read.me
#Git-tutorial
branch-a
$ git add read.me
$ git commit -m "ADD branch-a"
[branch-a 3b21508] ADD branch-a
1 file changed, 1 insertion(+), 1 deletion(-)
viエディタを使いましたが、なじみがない人はメモ帳でもなんでもOKです。
read.meに「branch-a」を一行追加しました。
addして、comittします。
masterはどうなっている
$ git checkout master
Switched to branch 'master'
$ cat read.me
#Git-tutorial
masterはどうなっているのか確認しましょう。当然ですが、「branch-a」は追加されていません。
masterにブランチをマージ
$ git checkout master
Switched to branch 'master'
$ git merge --no-ff branch-a
Merge made by the 'recursive' strategy.
read.me | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
branch-aの変更をmasterにマージします。--no-ffオプションにより、コメントにbranch-aをマージしたことが明記されます。これを実行するとviエディタが立ち上がりコメントの編集を求められます。よくわからない人はESCキー、:q!、ENTERキーで強制終了しましょう。
ブランチを視覚的に確認
$ git log --graph
* commit 8f5dd5856914c611b42d383d7839ee007e54ccda
|\ Merge: 261a8f8 3b21508
| | Author: firstname lastname <your_email@example.com>
| | Date: Mon Jul 11 22:23:11 2016 +0900
| |
| | Merge branch 'branch-a'
| |
| * commit 3b215082832159390120f2de503f98c14dd5edc4
|/ Author: firstname lastname <your_email@example.com>
| Date: Mon Jul 11 22:18:51 2016 +0900
|
| ADD branch-a
|
* commit 261a8f8b88b61ed304a0797c637d6a590fedccce
| Author: firstname lastname <your_email@example.com>
| Date: Sat Jul 9 14:54:58 2016 +0900
|
| my second commit
|
* commit 4833fd1ad12c674fb6d4583afb59a11193e759b4
Author: firstname lastname <your_email@example.com>
Date: Sat Jul 9 12:25:18 2016 +0900
my first commit