Branching Memo

# Introduction to Branching

以下のコマンドでブランチ作成、および HEAD も変更。

```
inohana:gitrepo inoko$ git branch featureA; git checkout featureA

inohana:gitrepo inoko$ git checkout -b featureB
```

```
inohana:gitrepo inoko$ git lga
* 29f7fef (HEAD -> master) modified
* af21070 test
* 33318c9 deleted
* 2cd810c discard
* 96e1c2d file copied
* 212efaf file moved
* c384fd1 line1 committed
* 5018a67 Third commit
* f5eb4f9 second modified.
* 2a7aa4b modified a file.
* b7daa30 create files.
inohana:gitrepo inoko$ git branch featureA; git checkout featureA

inohana:gitrepo inoko$ git checkout -b featureB
Switched to a new branch 'featureB'

inohana:gitrepo inoko$ git lga
* 29f7fef (HEAD -> featureB, master, featureA) modified
* af21070 test
* 33318c9 deleted
* 2cd810c discard
* 96e1c2d file copied
* 212efaf file moved
* c384fd1 line1 committed
* 5018a67 Third commit
* f5eb4f9 second modified.
* 2a7aa4b modified a file.
* b7daa30 create files.
* 
inohana:gitrepo inoko$ ls
barerepo	file3		file4		newfile2

# ファイルを2回更新
inohana:gitrepo inoko$ vi file3
inohana:gitrepo inoko$ git commit -am 'partially implemented featureB'
[featureB f5206e2] partially implemented featureB
 1 file changed, 3 insertions(+)
inohana:gitrepo inoko$ vi file3
inohana:gitrepo inoko$ git commit -am 'completed featureB'
[featureB 6bb18ef] completed featureB
 1 file changed, 2 insertions(+)

# HEAD が移動している
inohana:gitrepo inoko$ git lga
* 6bb18ef (HEAD -> featureB) completed featureB
* f5206e2 partially implemented featureB
* 29f7fef (master, featureA) modified
* af21070 test
* 33318c9 deleted
* 2cd810c discard
* 96e1c2d file copied
* 212efaf file moved
* c384fd1 line1 committed
* 5018a67 Third commit
* f5eb4f9 second modified.
* 2a7aa4b modified a file.
* b7daa30 create files.

# master に移動
inohana:gitrepo inoko$ git checkout master
Switched to branch 'master'
inohana:gitrepo inoko$ git lga
* 6bb18ef (featureB) completed featureB
* f5206e2 partially implemented featureB
* 29f7fef (HEAD -> master, featureA) modified
* af21070 test
* 33318c9 deleted
* 2cd810c discard
* 96e1c2d file copied
* 212efaf file moved
* c384fd1 line1 committed
* 5018a67 Third commit
* f5eb4f9 second modified.
* 2a7aa4b modified a file.
* b7daa30 create files.

inohana:gitrepo inoko$ ls
barerepo	file3		file4		newfile2

# feature3 での変更は反映されていない
inohana:gitrepo inoko$ cat file3
#01
line1
#02
#03
#04
#05
#06
#07
#08
line8
#09
#10
line10

# パラレルに編集
inohana:gitrepo inoko$ vi file3

inohana:gitrepo inoko$ git commit -am 'hotfix'
[master 6f454e0] hotfix
 1 file changed, 1 insertion(+)


# 分岐した
inohana:gitrepo inoko$ git lga
* 6f454e0 (HEAD -> master) hotfix
| * 6bb18ef (featureB) completed featureB
| * f5206e2 partially implemented featureB
|/
* 29f7fef (featureA) modified
* af21070 test
* 33318c9 deleted
* 2cd810c discard
* 96e1c2d file copied
* 212efaf file moved
* c384fd1 line1 committed
* 5018a67 Third commit
* f5eb4f9 second modified.
* 2a7aa4b modified a file.
* b7daa30 create files.
inohana:gitrepo inoko$
```

# Merging Feature Branches
統合のベストプラクティスは、Featureラインをメインラインに統合することです。
まずはメインラインの変更をFeatureブランチに統合。
全ての統合は、HEAD に向かって行われます。
そのため、まずは Featureブランチに移動します。

```
inohana:gitrepo inoko$ git lga
* 6f454e0 (HEAD -> master) hotfix
| * 6bb18ef (featureB) completed featureB
| * f5206e2 partially implemented featureB
|/
* 29f7fef (featureA) modified
* af21070 test
* 33318c9 deleted
* 2cd810c discard
* 96e1c2d file copied
* 212efaf file moved
* c384fd1 line1 committed
* 5018a67 Third commit
* f5eb4f9 second modified.
* 2a7aa4b modified a file.
* b7daa30 create files.
inohana:gitrepo inoko$ git checkout featureB
Switched to branch 'featureB'
inohana:gitrepo inoko$ git merge master
Auto-merging file3
Merge made by the 'recursive' strategy.
 file3 | 1 +
 1 file changed, 1 insertion(+)
inohana:gitrepo inoko$ git lga
*   45ada03 (HEAD -> featureB) Merge branch 'master' into featureB
|\
| * 6f454e0 (master) hotfix
* | 6bb18ef completed featureB
* | f5206e2 partially implemented featureB
|/
* 29f7fef (featureA) modified
* af21070 test
* 33318c9 deleted
* 2cd810c discard
* 96e1c2d file copied
* 212efaf file moved
* c384fd1 line1 committed
* 5018a67 Third commit
* f5eb4f9 second modified.
* 2a7aa4b modified a file.
* b7daa30 create files.
inohana:gitrepo inoko$ ls
barerepo	file3		file4		newfile2
inohana:gitrepo inoko$ cat file3
#01
line1
#02
#03
line3
#04
line4
#05
line5
#06
line6
#07
line7
#08
line8
#09
#10
line10
hotfix
inohana:gitrepo inoko$ git checkout master
Switched to branch 'master'

# master ポイントが移動するだけなのでコミットメッセージは求められない
inohana:gitrepo inoko$ git merge featureB
Updating 6f454e0..45ada03
Fast-forward
 file3 | 5 +++++
 1 file changed, 5 insertions(+)

inohana:gitrepo inoko$ git lga
*   45ada03 (HEAD -> master, featureB) Merge branch 'master' into featureB
|\
| * 6f454e0 hotfix
* | 6bb18ef completed featureB
* | f5206e2 partially implemented featureB
|/
* 29f7fef (featureA) modified
* af21070 test
* 33318c9 deleted
* 2cd810c discard
* 96e1c2d file copied
* 212efaf file moved
* c384fd1 line1 committed
* 5018a67 Third commit
* f5eb4f9 second modified.
* 2a7aa4b modified a file.
* b7daa30 create files.
```
# Branch Management

```
inohana:gitrepo inoko$ git branch
  featureA
  featureB
* master
inohana:gitrepo inoko$ git branch -v
  featureA 29f7fef modified
  featureB 45ada03 Merge branch 'master' into featureB
* master   45ada03 Merge branch 'master' into featureB

# remote のブランチ含めて表示できる
inohana:fsd01 inoko$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

inohana:gitrepo inoko$ git branch --no-merged
inohana:gitrepo inoko$ git branch --merged
  featureA
  featureB
* master

# -d オプションでブランチ削除可能だが、マージされていないブランチは削除できない仕様になっているので安心
# どうしても消したい場合には、 -D オプションで削除可能。

inohana:gitrepo inoko$ git branch -d featureA
Deleted branch featureA (was 29f7fef).
inohana:gitrepo inoko$

```