522
518

More than 5 years have passed since last update.

ブランチ切って更新してマージするまでの流れ

Posted at

前提

  • masterブランチを本番環境で適用する想定
  • developブランチをstg環境で適用する想定

今回は、developブランチに機能を追加するため、feature-create-apiというブランチを作成し、後でdevelopブランチにマージすることを考える。

1.developブランチからfeature-create-apiを作成

1-1.developブランチへ移動

git checkout develop

1-2.feature-create-apiを作成し、移動する

git checkout -b feature-create-api
  • -bオプションをつけると、移動とセットでブランチの作成も出来る
  • 現在のブランチはgit branchで確認可能

(おまけ)ブランチの削除

もしブランチ名を間違えてfeature-creaxxxとしてしまったら、消して作りなおそう!

まず、消したいブランチとは異なるブランチに移動し、ブランチを消す。

git checkout develop
git checkout -d feature-creaxxx

2.feature-create-apiブランチで作業

実装後、

git add .
git commit -am "commit message!"
git push origin feature-create-api

などとして、remoteに反映。

3.developへのマージ作業

3-1.マージ先のブランチに移動

git checkout develop

3-2.マージ

git merge --no-ff feature-create-api
  • --no-ffオプション:fast-forwardの関係であっても、必ずマージコミットを作る
  • 機能追加が見やすいので、僕らのプロジェクトではこの--no-ffオプションを使っている
  • mergeする時、ローカルのdevelopブランチを最新の状態にするのを忘れずに!(以前筆者は躓いた)

▼(参考)図で分かるgit-mergeの--ff, --no-ff, --squashの違い
http://d.hatena.ne.jp/sinsoku/20111025/1319497900

おまけ1

おすすめのgit運用方法

普段gitのブランチをなるべく枝分かれさせないようにし、機能追加の単位でブランチを切るのが個人的なおすすめ。

他の人とpushが交互になる場合、

git fetch origin
git rebase origin/master

とすれば、ブランチが分かれず1本につながる。

▼(参考)git rebase 失敗した時の対処法
http://qiita.com/shuntaro_tamura/items/c505b76c1021a35ca9ff

機能追加の時は、上記のようにfeature-xxxブランチを切って

git merge --no-ff feature-xxx

によりマージするのがおすすめです。

おまけ2(自分用メモ)

developへのマージ

git checkout develop
git merge --no-ff feature-xxx

masterへのプルリク

ブラウザのgithubからプルリク(プルリク後マージされる)
mvn clean deploy
522
518
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
522
518