Gitでブランチの作成とマージと削除を試してみる
ブランチの作成とマージと削除を試してみる。
現状
現状、sample.txtのファイルが1つある状態。
$ cat sample.txt 111 222 333
$ git status # On branch master nothing to commit (working directory clean)
ブランチの作成
git branch ブランチ名
でブランチが作成できる。
$ git branch test
git checkout ブランチ名
で作成したブランチに切り替えられる。
$ git checkout test Switched to branch 'test'
git checkout -b ブランチ名
のように「-b」をつけると、ブランチの作成とブランチへの切り替えが同時に行うことができる。
git branch
で、現在のブランチを確認できる。
$ git branch
master
* test
ブランチのマージ
ファイルを修正し、コミットする。
$ vi sample.txt 111 222 333 444
$ git commit -a -m '444を追加' [test 6afc755] 444を追加 1 files changed, 1 insertions(+), 0 deletions(-)
masterブランチに切り替える。
$ git checkout master Switched to branch 'master'
sample.txtは修正される前のまま。
$ cat sample.txt 111 222 333
git merge ブランチ名
でブランチの内容をマージできる。
$ git merge test Updating 83d4ba0..6afc755 Fast-forward sample.txt | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
ブランチの修正が反映された。
$ cat sample.txt 111 222 333 444
ブランチの削除
git branch -d ブランチ名
でブランチを削除することができる。
$ git branch -d test Deleted branch test (was 6afc755).
ブランチが無くなってるのが確認できる。
$ git branch * master
早送りのマージ
masterに変更がない状態でmergeすると、早送り(Fast-Forward)のマージがされ
グラフは下記のようになる。
$ git log --graph * commit 6afc755a74e064fa5978bd64aed2251d0975a2b8 | Author: Your Name <you@example.com> | Date: Mon Mar 3 15:13:13 2014 +0900 | | 444を追加 |
改めてブランチを作成して修正をコミットする。
$ git checkout -b test Switched to a new branch 'test' $ vi sample.txt 111 222 333 444 555 $ git commit -a -m '555を追加' [test 1e98f92] 555を追加 1 files changed, 1 insertions(+), 0 deletions(-)
masterブランチに切り替えて、
$ git checkout master Switched to branch 'master' Your branch is ahead of 'origin/master' by 1 commit.
今度は「--no-ff」を付けてマージすると、
$ git merge --no-ff test Merge made by recursive. sample.txt | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
コミットグラフは下記のようになり、
早送りじゃない、普通のマージがされていることが分かる。
$ git log --graph * commit 2cfb9cb90152d7b2421e9523d91efcc03ea93957 |\ Merge: 6afc755 1e98f92 | | Author: Your Name <you@example.com> | | Date: Mon Mar 3 15:38:19 2014 +0900 | | | | Merge branch 'test' | | | * commit 1e98f925e5d6b98ae0c6dd2147241ae54a930677 |/ Author: Your Name <you@example.com> | Date: Mon Mar 3 15:37:24 2014 +0900 | | 555を追加 | * commit 6afc755a74e064fa5978bd64aed2251d0975a2b8 | Author: Your Name <you@example.com> | Date: Mon Mar 3 15:13:13 2014 +0900 | | 444を追加 |
逆に「--ff-only」を付けると、常に早送りのマージとなる。
できない場合はエラーになるみたい。
【参考】 こわくない Git http://www.slideshare.net/kotas/git-15276118