git-flow に入門する
git-flowをインストールをする為に以下のコマンドを実行します。
brew install git-flowgit-flowの設定を初期化する為に以下のコマンドを実行します。
git flow init強制的に初期化する場合は -f をオプションとしてつけます。
git flow init -f※ステージングしていない変更があるとgit-flowは初期化はできません。
fatal: Working tree contains unstaged changes. Aborting.git-flow を実行後、対話形式で設定に関する質問がなされます。
以下のように設定します。
| 項目 | 設定値 | 説明 |
|---|---|---|
| Branch name for production releases | main | リリース用のブランチ名 |
| Branch name for "next release" development | develop | 開発用のブランチ名 |
| Feature branches? | feature/ | 作業用ブランチ名のプレフィックス |
| Release branches? | release/ | リリース用ブランチ名のプレフィックス |
| Hotfix branches? | hotfix/ | hotfix |
| Support branches? | support/ | support |
| Version tag prefix? | tag | tag |
git-flowを実行するリポジトリを用意します。
この時に利用するリポジトリはテストで使用しても良い自分のリポジトリであれば、なんでも構いません。
ここではgitflow-beginnerのリモートリポジトリを利用します。
以下のコマンドを実行します。
git clone https://github.com/ymd65536/gitflow-beginner.git
cd gitflow-beginnerセットアップが完了したことを確認する為に featureのtestブランチを切ります。
以下のコマンドを実行します。
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 testfeature/testブランチがdevelopをベースに作成されます。
git branchでbranchの一覧を表示します。
git branchコマンドを実行すると以下のように実行結果が出力されます。
作業用ブランチのfeature/testが作成されていることを確認します。
develop
* feature/test
main作業ブランチをローカル上の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 testpublish実行後はpush でGitHub上の作業ブランチを更新します。以下のコマンドを実行します。
git push origin feature/testGitHub 上でマージ作業を完了する場合は以降のコマンドは利用しません。
ローカル上で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'リモートリポジトリにブランチをpushします。以下のコマンドを実行します。
git push origin develop| コマンド | 説明 |
|---|---|
| git init | リポジトリを初期化する |
| git add | リポジトリ内にある変更をステージングする |
| git commit -m "コメント" | ステージングされた変更をコメント付きでブランチに適用する |
| git checkout -b <ブランチ名> | 指定した名前でブランチを切る |
| git checkout <ブランチ名> | 指定したブランチ名でブランチを切り替える |
| git branch -d <ブランチ名> | ブランチを削除する |
| git pull origin <リモートブランチ>:<ローカルブランチ> | リモートリポジトリに存在するブランチの変更を取得し、指定のローカルブランチに反映する |