gitflow-beginner

git-flow に入門する

git-flow のセットアップ

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

git-flowをインストールをする為に以下のコマンドを実行します。

brew install git-flow

git-flow を初期化する

git-flowの設定を初期化する為に以下のコマンドを実行します。

git flow init

強制的に初期化する場合は -f をオプションとしてつけます。

git flow init -f

※ステージングしていない変更があるとgit-flowは初期化はできません。

fatal: Working tree contains unstaged changes. Aborting.

git-flow を設定する

git-flow を実行後、対話形式で設定に関する質問がなされます。
以下のように設定します。

項目設定値説明
Branch name for production releasesmainリリース用のブランチ名
Branch name for "next release" developmentdevelop開発用のブランチ名
Feature branches?feature/作業用ブランチ名のプレフィックス
Release branches?release/リリース用ブランチ名のプレフィックス
Hotfix branches?hotfix/hotfix
Support branches?support/support
Version tag prefix?tagtag

動作確認用のGitリポジトリをクローンする

git-flowを実行するリポジトリを用意します。
この時に利用するリポジトリはテストで使用しても良い自分のリポジトリであれば、なんでも構いません。

ここではgitflow-beginnerのリモートリポジトリを利用します。
以下のコマンドを実行します。

git clone https://github.com/ymd65536/gitflow-beginner.git
cd gitflow-beginner

git-flow をテストする

セットアップが完了したことを確認する為に featuretestブランチを切ります。
以下のコマンドを実行します。

git flow feature start test

コマンドを実行すると以下のように実行結果が出力されます。

Switched to a new branch 'feature/test'

Summary of actions:
- A new branch 'feature/test' was created, based on 'develop'
- You are now on branch 'feature/test'

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

     git flow feature finish test

feature/testブランチがdevelopをベースに作成されます。

git branchbranchの一覧を表示します。

git branch

コマンドを実行すると以下のように実行結果が出力されます。
作業用ブランチのfeature/testが作成されていることを確認します。

  develop
* feature/test
  main

GitHubに作業ブランチを publishする

作業ブランチをローカル上のdevelopでマージせず、GitHub上でマージする場合は
git flow feature finishではなくgit flow feature publishを実行します。

git flow feature finishではdevelopに作業ブランチfeature/testをマージします。

git flow feature finishを実行してしまうと作業ブランチは削除されてしまい、GitHub上でdevelopブランチとの差分比較ができません。
ゆえに、GitHubでプルリクエストを作成してレビューする場合はgit flow feature publishを実行して作業ブランチをGitHub上にpushする必要があります。

git flow feature publish test

publish実行後はpush でGitHub上の作業ブランチを更新します。以下のコマンドを実行します。

git push origin feature/test

GitHub 上でマージ作業を完了する場合は以降のコマンドは利用しません。
ローカル上でdevelopブランチの作業を実行したい場合はローカル上で作業ブランチをdevelopブランチにマージする必要があります。

作業ブランチを developブランチ にマージする

ローカル上でdevelopブランチと作業ブランチfeature/testをマージする場合は以下のコマンドを実行します。
※このコマンドの実行によって、作業ブランチがdevelopにマージされます。作業ブランチは削除されます。

git flow feature finish test

コマンドを実行すると以下のように実行結果が出力されます。

Switched to branch 'develop'
Updating 74c9ab8..20e9cd7
Fast-forward
 README.md | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
Deleted branch feature/test (was 20e9cd7).

Summary of actions:
- The feature branch 'feature/test' was merged into 'develop'
- Feature branch 'feature/test' has been removed
- You are now on branch 'develop'

developブランチをリモートリポジトリにpushする

リモートリポジトリにブランチをpushします。以下のコマンドを実行します。

git push origin develop

Tips

Gitの基本

コマンド説明
git initリポジトリを初期化する
git addリポジトリ内にある変更をステージングする
git commit -m "コメント"ステージングされた変更をコメント付きでブランチに適用する
git checkout -b <ブランチ名>指定した名前でブランチを切る
git checkout <ブランチ名>指定したブランチ名でブランチを切り替える
git branch -d <ブランチ名>ブランチを削除する
git pull origin <リモートブランチ>:<ローカルブランチ>リモートリポジトリに存在するブランチの変更を取得し、指定のローカルブランチに反映する