入門hubflow 

# Hubflow

今更感は満載ですが、普段よく利用している、 [HubFlow](https://datasift.github.io/gitflow/) の導入方法とリリースまでの流れをまとめてみました。基本的には、Hubflow や [ゆううきブログ](https://blog.yuuk.io/entry/hubflow)  に書いてあるとおりなのですが、どのような動きになるのか、あらためて紹介したいと思います。

なお、gitflow そのものについては、「[ここ](https://qiita.com/KosukeSone/items/514dd24828b485c69a05)」とか「[ここ](http://d.hatena.ne.jp/Yamashiro0217/20120903/1346640190)」が参考になると思います。

# Hubflow インストール

以下のコマンドで導入します。

```shell
➤ git clone https://github.com/datasift/gitflow
➤ cd gitflow
➤ sudo ./install.sh
```

使い方は、`git hf` で確認できます。

```shell
➤  git hf
usage: git hf <subcommand>

Available subcommands are:
   init      Initialize a new git repo with support for the branching model.
   feature   Manage your feature branches.
   release   Manage your release branches.
   hotfix    Manage your hotfix branches.
   push      Push the changes from your current branch (plus any new tags) back upstream.
   pull      Pull upstream changes down into your master, develop, and current branches.
   update    Pull upstream changes down into your master and develop branches.
   version   Shows version information.

Try 'git hf <subcommand> help' for details.
```


# Github 上にレポジトリを作成

新規に開発を始める場合、 Github上でレポジトリを作成します。
[サンプル](https://github.com/wind-up-bird/hubflow-sample)
今回は、レポジトリを `hubflow-samle` としています。

# リリースまでの流れ

### レポジトリの Clone

まずは、リモートレポジトリをローカル環境に Clone します。

```
➤ git clone git@github.com:wind-up-bird/hubflow-sample
➤ cd hubflow-sample/
➤  git branch
* master
```

### ローカルのレポジトリを初期化

Clone したレポジトリを初期化します。

```shell
➤  git hf init
Using default branch names.

Which branch should be used for tracking production releases?
   - master
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? []
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'develop' on GitHub by visiting:
remote:      https://github.com/wind-up-bird/hubflow-sample/pull/new/develop
remote:
To ssh://github.com/wind-up-bird/hubflow-sample
 * [new branch]      develop -> develop
```

この時点で、ローカルに develop ブランチが作成されます。

```shell
➤ git branch -a
* develop
  master
  remotes/origin/HEAD -> origin/develop
  remotes/origin/develop
  remotes/origin/master
```

また、リモートに develop ブランチが存在しない場合は、新規に作成さます。

### Feature branch の作成

次のコマンドで feature ブランチ (例: `sample_feature`) を作成します。

```shell
➤ git hf feature start sample_feature
Fetching origin
Switched to a new branch 'feature/sample_feature'
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'feature/sample_feature' on GitHub by visiting:
remote:      https://github.com/wind-up-bird/hubflow-sample/pull/new/feature/sample_feature
remote:
To github.com:wind-up-bird/hubflow-sample
 * [new branch]      feature/sample_feature -> feature/sample_feature

Summary of actions:
- A new branch 'feature/sample_feature' was created, based on 'develop'
- The branch 'feature/sample_feature' has been pushed up to 'origin/feature/sample_feature'
- You are now on branch 'feature/sample_feature'

Now, start committing on your feature. When done, create a
pull request on GitHub.  Once that has been merged, use:

     git hf feature finish sample_feature
```

ローカルおよびリモートブランチに `sample_feature` が作成されます。

```shell
➤ git br -a
  develop
* feature/sample_feature
  master
  remotes/origin/HEAD -> origin/develop
  remotes/origin/develop
  remotes/origin/feature/sample_feature
  remotes/origin/master
```

ちなみに、Github上では、以下のような状態になります。

![Screen Shot 2018-11-18 at 22.18.33.png](https://qiita-image-store.s3.amazonaws.com/0/118641/4288803a-3661-f306-4311-cfcf69ab7ea4.png)

### Commit & Push

ここでは、適当にファイルを作成し、github にpushしてみます。

テスト用に `hello_world.txt` を作成し、

```
➤ echo 'Hello World.' hello_world.txt
Hello World. hello_world.txt
```

Github に push します。


```shell
➤ git add .
➤ git commit -m "sample comment"
➤ git hf push
Fetching origin
Already up to date.
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 312 bytes | 312.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:wind-up-bird/hubflow-sample
   f7623c7..26ebf3a  feature/sample_feature -> feature/sample_feature

Summary of actions:
- The remote branch 'origin/feature/sample_feature' was updated with your changes
```

### PR 作成 & Review

Github 上で Pull Request ([サンプル](https://github.com/wind-up-bird/hubflow-sample/pull/1)) を作成し、Review を依頼します。
Review が終了したら、develop ブランチに merge します。

### 【補足】 他の人が開発に参加する場合 

他の人が `sample_feature` の開発に参加する場合は、レポジトリをCloneして、 `git hf init` で初期化します。
その後、

```bash
➤ git hf feature checkout sample_feature
```

とすることで、ローカルに feature ブランチを作成、開発に参加することが可能です。


### Feature ブランチの削除

PRも無事 merge され、`sample_feature` の開発が終了したら、

```bash
➤ git hf feature finish sample_feature
Fetching origin
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (1/1), done.
From github.com:wind-up-bird/hubflow-sample
   f7623c7..cd66bde  develop    -> origin/develop
Updating f7623c7..cd66bde
Fast-forward
 hello_world.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 hello_world.txt
Already up to date.
To github.com:wind-up-bird/hubflow-sample
 - [deleted]         feature/sample_feature
Deleted branch feature/sample_feature (was 26ebf3a).

Summary of actions:
- The latest changes from 'origin' were merged into 'master' and 'develop'
- The feature branch 'feature/sample_feature' was merged into 'develop'
- Feature branch 'feature/sample_feature' has been removed
- Feature branch 'origin/feature/sample_feature' has been removed
- You are now on branch 'develop'
```

で、ローカルとリモートのブランチを削除します。
このとき、Github 上では、以下のような状態になると思います。

![Screen Shot 2018-11-18 at 22.58.57.png](https://qiita-image-store.s3.amazonaws.com/0/118641/b8d716f6-150b-e92b-fe76-bd99af48cd38.png)

### Release ブランチの作成

まず、ローカルの master と develop を upstream に追従させます。

```bash
➤ git br -a
* develop
  master
  remotes/origin/HEAD -> origin/develop
  remotes/origin/develop
  remotes/origin/master
```

```bash
➤ git hf update
Fetching origin

Summary of actions:
- Any changes to branches at origin have been downloaded to your local repository
- Any branches that have been deleted at origin have also been deleted from your local repository
- Any changes from origin/master have been merged into branch 'master'
- Any changes from origin/develop have been merged into branch 'develop'
- Any resolved merge conflicts have been pushed back to origin
- You are now on branch 'develop'
```

次に、リリースバージョンを `0.0.1` として、ローカルとリモートにリリースブランチを作成します。

```bash
➤ git hf release start 0.0.1
Fetching origin
Switched to a new branch 'release/0.0.1'
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'release/0.0.1' on GitHub by visiting:
remote:      https://github.com/wind-up-bird/hubflow-sample/pull/new/release/0.0.1
remote:
To github.com:wind-up-bird/hubflow-sample
 * [new branch]      release/0.0.1 -> release/0.0.1

Summary of actions:
- A new branch 'release/0.0.1' was created, based on 'develop'
- The branch 'release/0.0.1' has been pushed up to 'origin/release/0.0.1'
- You are now on branch 'release/0.0.1'

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

     git hf release finish '0.0.1'
```

ここでは、

1. develop ブランチを元に `release/0.0.1` ブランチの作成
2. `release/0.0.1` ブランチを push

しています。

# Release 

次のコマンドでリリースします。

```
➤ git hf release finish '0.0.1'
```

途中、コミットメッセージ編集のためエディタが起動するので、適宜入力します。

```
Merge tag '0.0.1' into develop

merge master

# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
```

コマンド例:

```
➤ git hf release finish '0.0.1'
Fetching origin
Fetching origin
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
Merge made by the 'recursive' strategy.
 hello_world.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 hello_world.txt
Switched to branch 'develop'
Your branch is up to date with 'origin/develop'.
Already up to date!
Merge made by the 'recursive' strategy.
Deleted branch release/0.0.1 (was cd66bde).
Enumerating objects: 2, done.
Counting objects: 100% (2/2), done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 367 bytes | 367.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To github.com:wind-up-bird/hubflow-sample
   cd66bde..d01054c  develop -> develop
Total 0 (delta 0), reused 0 (delta 0)
To github.com:wind-up-bird/hubflow-sample
   f7623c7..dec5ac0  master -> master
Enumerating objects: 1, done.
Counting objects: 100% (1/1), done.
Writing objects: 100% (1/1), 179 bytes | 179.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To github.com:wind-up-bird/hubflow-sample
 * [new tag]         0.0.1 -> 0.0.1
To github.com:wind-up-bird/hubflow-sample
 - [deleted]         release/0.0.1

Summary of actions:
- Latest objects have been fetched from 'origin'
- Release branch has been merged into 'master'
- The release was tagged '0.0.1'
- Tag '0.0.1' has been back-merged into 'develop'
- Branch 'master' has been back-merged into 'develop'
- Release branch 'release/0.0.1' has been deleted
- 'develop', 'master' and tags have been pushed to 'origin'
- Release branch 'release/0.0.1' in 'origin' has been deleted.
```

Summary of actions にある通り、 ここでは、

1. リモートの変更をすべて取得
2. リリースブランチを master に merge
3. リリースタグ `0.0.1` を作成
4. タグ `0.0.1` を develop と master に merge
5. master ブランチを develop に merge
6. `release/0.0.1` ブランチを削除
7. develop, master とタグをリモートにプッシュ
8. リモートの `release/0.0.1` を削除

Github 上では以下のような状態になると思います。
![Screen Shot 2018-11-18 at 23.23.27.png](https://qiita-image-store.s3.amazonaws.com/0/118641/69907765-db1c-133c-cbc5-30fd836849d9.png)

---

ということで、Hubflow を利用したリリースまでの流れを紹介しました。

こちらからは以上です。