2012年6月28日木曜日

「git-flowによるブランチ管理」を試してみる

git を使いこなすのが難しいと感じる今日このごろ。
皆様いかがお過ごしでしょうか。
gitを使っていると作業中のブランチどうするのとかいろいろ気になりますね。
でも、ブランチの管理って複雑なんだよなーとか・・・思っていたらオライリーのサイトで「git-flowによるブランチ管理」という目からうろこのような記事がありました。
ブランチ管理が共通化できて便利そうなので試してみること・・・。

ちなみにgit-daliyというGREEのインフラの方(sotarokさん)が作成したものもあるようです。
こちらは、PHPで作られているようですね。

参考

git-flow によるブランチの管理
A successful Git branching model(日本語訳)
gitflow

コマンドが出力するメッセージも合わせて記述しているので、多少見にくいかもしれません。
内容的にあまり変わりないと思うので、試したい場合は、上記参考サイトの「git-flowによるブランチ管理」をおすすめいたします。

試した環境

CentOS 6.2
Git version 1.7.11.1 (2012/6/28現在のリリースバージョン)

1. git-flowをインストールする

いきなり少し違いますが、この環境では、gitを/usr/local/gitにインストールしたので、コマンドにINSTALL_PREFIXオプションつけています。
sudo su -
cd /usr/local/src
wget --no-check-certificate -q -O - https://github.com/nvie/gitflow/raw/develop/contrib/gitflow-installer.sh | env INSTALL_PREFIX=/usr/local/git/bin bash
chmod -R 755 /usr/local/git/bin

2. 空のリポジトリを作成する

git init --bare --shared=group foobar.git
cp foobar.git/hooks/post-update.sample foobar.git/hooks/post-update

3. 作業用のディレクトリに移動して空のリポジトリをcloneする

git clone /var/git/foobar.git

4. git-flowを導入する

cd foobar
git flow init
デフォルトのままEnterキーを押し続けます。
次の内容を聞かれますので必要があれば適宜で変更してください。
No branches exist yet. Base branches must be created now.
Branch name for production releases: [master]
Branch name for "next release" development: [develop]
How to name your supporting branch prefixes?
Feature branches? [feature/]
Release branches? [release/]
Hotfix branches? [hotfix/]
Support branches? [support/]
Version tag prefix? []
これなら既存のプロジェクトにも導入できますね。

5. 機能(helloworld)を追加する

git flow feature start helloworld
Switched to a new branch 'feature/helloworld'
Summary of actions:
- A new branch 'feature/helloworld' was created, based on 'develop'
- You are now on branch 'feature/helloworld'

Now, start committing on your feature. When done, use:

git flow feature finish helloworld

6. ブランチを確認する

git branch
develop
* feature/helloworld
master

7. 機能を追加する

vi helloworld.pl
#!/usr/bin/perl

use strict;
use warnings;

print "Hello World\n";

8. リポジトリに追加する

git add helloworld.pl
git commit -m "HelloWorld"

[feature/helloworld 0fabb48] HelloWorld
 1 file changed, 7 insertions(+)
 create mode 100755 helloworld.pl
コミットしました。

9. 開発用ブランチ(develop)にマージする

git flow feature finish helloworld

Switched to branch 'develop'
Updating e69c50f..0fabb48
Fast-forward
 helloworld.pl | 7 +++++++
 1 file changed, 7 insertions(+)
 create mode 100755 helloworld.pl
 Deleted branch feature/helloworld (was 0fabb48).
 
 Summary of actions:
 - The feature branch 'feature/helloworld' was merged into 'develop'
 - Feature branch 'feature/helloworld' has been removed
 - You are now on branch 'develop'
上記のメッセージのとおり、
・feature/helloworldブランチがdevelopブランチにマージします。
・feature/helloworldブランチは削除されます。
・developブランチに切り替わります。

10. ブランチを確認する

git branch
* develop
  master
確かにそのとおりです。

11. developブランチをpushする

git push origin develop

Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (5/5), 438 bytes, done.
Total 5 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (5/5), done.
To /var/git/foobar.git
 * [new branch]      develop -> develop

12. ver1.0.0としてリリースブランチを作成する

git flow release start 1.0.0

Switched to a new branch 'release/1.0.0'
Summary of actions:
- A new branch 'release/1.0.0' was created, based on 'develop'
- You are now on branch 'release/1.0.0'

Follow-up actions:- Bump the version number now!
- Start committing last-minute fixes in preparing your release
- When done, run:

  git flow release finish '1.0.0'

13. ブランチを確認

git branch
  develop
  master
  * release/1.0.0

14. READMEを作成する

vi README
README
====================================

- 1.0.0: print "Hello World"

14. リリースする

git add README
git commit -m 'update readme for release 1.0.0'

[release/1.0.0 e331ab0] update readme for release 1.0.0
 1 file changed, 4 insertions(+)
 create mode 100644 README
 
 git flow release finish 1.0.0
 
 # エディタが開きマージの内容やtagの入力など求められるので入力して保存する
 
 Switched to branch 'master'
 Merge made by the 'recursive' strategy.
 README        | 4 ++++
 helloworld.pl | 7 +++++++<
 2 files changed, 11 insertions(+)
 create mode 100644 README
 create mode 100755 helloworld.pl
 Switched to branch 'develop'
 Merge made by the 'recursive' strategy.
 README | 4 ++++
 1 file changed, 4 insertions(+)
 create mode 100644 README
 Deleted branch release/1.0.0 (was e331ab0).
 
 Summary of actions:
 - Latest objects have been fetched from 'origin'
 - Release branch has been merged into 'master'
 - The release was tagged '1.0.0'
 - Release branch has been back-merged into 'develop'
 - Release branch 'release/1.0.0' has been deleted

15. ブランチを確認する

git branch
* develop
master

16. タグを確認する

git tag
1.0.0

17. pushする

git push

Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 442 bytes, done.
Total 4 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (4/4), done.
To /var/git/foobar.git
  0fabb48..e006137  develop -> develop

18. タグをpushする

git push origin 1.0.0

Counting objects: 2, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 342 bytes, done.
Total 2 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (2/2), done.
To /var/git/foobar.git
* [new tag]         1.0.0 -> 1.0.0

19. 修正用のブランチを作成する

git flow hotfix start 1.0.1-bugfix

Switched to a new branch 'hotfix/1.0.1-bugfix'

Summary of actions:
- A new branch 'hotfix/1.0.1-bugfix' was created, based on 'master'
- You are now on branch 'hotfix/1.0.1-bugfix'

Follow-up actions:
- Bump the version number now!
- Start committing your hot fixes
- When done, run:

  git flow hotfix finish '1.0.1-bugfix'

20. ファイルを修正する

次のようにhelloworld.plに1行追加しました。
git diff
diff --git a/helloworld.pl b/helloworld.pl
index 2382e77..6fa1737 100755
--- a/helloworld.pl
+++ b/helloworld.pl
@@ -4,4 +4,5 @@ use strict;
 use warnings;
 
 print "Hello World\n";
 +print "BugFix\n";

21. 修正版をコミットする

git commit helloworld.pl -m "Bugfix."

[hotfix/1.0.1-bugfix be609ab] Bugfix.
 1 file changed, 1 insertion(+)

22. 修正版を適用する

git flow hotfix finish 1.0.1-bugfix

# マージする内容の確認やタグの入力など求められるので入力して保存する

Switched to branch 'master'
Merge made by the 'recursive' strategy.
 helloworld.pl | 1 +
 1 file changed, 1 insertion(+)
Switched to branch 'develop'
Merge made by the 'recursive' strategy.
 helloworld.pl | 1 +
 1 file changed, 1 insertion(+)
Deleted branch hotfix/1.0.1-bugfix (was be609ab).

Summary of actions:
- Latest objects have been fetched from 'origin'
- Hotfix branch has been merged into 'master'
- The hotfix was tagged '1.0.1-bugfix'
- Hotfix branch has been back-merged into 'develop'
- Hotfix branch 'hotfix/1.0.1-bugfix' has been deleted

23. タグの確認

git tag
1.0.0
1.0.1-bugfix

24. pushする

git push

Counting objects: 6, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 501 bytes, done.
Total 4 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (4/4), done.
To /var/git/foobar.git
   e006137..da2ab95  develop -> develop

git push origin 1.0.1-bugfix

Counting objects: 2, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 355 bytes, done.
Total 2 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (2/2), done.
To /var/git/foobar.git
 * [new tag]         1.0.1-bugfix -> 1.0.1-bugfix
git flowすばらしいです。
私のようなブランチ管理が苦手な人でも使えそうです。

1点気になったのは、リリースのバージョン情報ですね。
細かい修正が多い場合は数値がどんどん追加されてカオスになりそうですね。
そのあたりの運用ポリシーをどうするかが問題になりそうです。

0 件のコメント:

コメントを投稿