DEV Community

okash1n
okash1n

Posted on

2 2

Github Flowの覚書

最近はRailsチュートリアルやProgateのRails講座をやっています。お恥ずかしながら、あまり仕事でgitを使うことが無かったので、Github Flowなどもよく理解していません。というわけでProgateのRails講座をブラウザでやりながら、ローカルでもRails立ち上げて、1問ごとにブランチ切って自分でプルリク出して開発してみることにしました。
やっていく中で結構覚えてきたので、メモとして残しておこうと思います。

Github Flowの大まかな流れ

大まかには下記のような流れです。

  1. masterブランチから新しくブランチを切る
  2. 変更を加える
  3. commitする
  4. pushする
  5. プルリクエストを出す
  6. レビューしてもらう
  7. 問題なければマージする(してもらう)
  8. リモートブランチを削除する

詳細な手順

ここでは、プルリクエストの部分以外はローカルからコマンドでやる方法を記載します。
新しい機能(new-feature)を追加します。コマンドは全てプロジェクトのルートディレクトリで実行しているものとします。

ブランチの作成

  • ブランチを切ってそのブランチに移動する
    • ブランチ名はチームのルールに従って、追加する変更がわかりやすい名前にする(Jiraなどのタスクナンバーにする場合も)
    • git checkout -b new-feature
> git branch -a
  master #ローカルのマスター
* new-feature #現在のブランチ
  remotes/master #リモートのマスター
Enter fullscreen mode Exit fullscreen mode

変更からプルリクまで

  • 変更を加える
  • commitする
    • git add .
    • git commit -m "新しい機能を追加"
  • リモートブランチにpushする
    • git push origin new-feature
> git branch -a
  master
* new-feature #現在のブランチ
  remotes/new-feature #リモートに同名のブランチが作成された
  remotes/master
Enter fullscreen mode Exit fullscreen mode
  • プルリクエストを出す
    • ブラウザでリポジトリを開き、リモートブランチ(new-feature)に対しプルリクエストを出す
    • pushした直後であればリポジトリのトップ画面にCompare & pull requestというボタンが出ている Screenshot from 2018-07-22 23-03-34.png (49.9 kB)
  • レビュアーにレビューしてもらう

変更の取り込み

自身でマージする場合は【自分でマージする場合】まで飛ばしてください

他の人(レビュアーなど)がマージする場合

> git branch -a
* master
  remotes/master
  remotes/new-feature # プルリクのコミット
Enter fullscreen mode Exit fullscreen mode
  • ローカルのorigin/masterを更新する
    • git fetch origin

参考
git fetchの理解からgit mergeとpullの役割

  • ローカルにプルリクのブランチを持ってくる(リモートブランチからブランチを作成)
    • git checkout -b new-feature origin/new-feature
> git branch -a
  master
* new-feature
  remotes/master
  remotes/new-feature
Enter fullscreen mode Exit fullscreen mode

自分でマージする場合

  • masterに移動
    • git branch master
> git branch -a
* master #現在のブランチ
  new-feature
  remotes/master
  remotes/new-feature
Enter fullscreen mode Exit fullscreen mode
  • 変更(new-feature)をローカルのmasterにマージする
    • git merge --no-ff new-feature
> git branch -a
* master  #同じコミット
  new-feature #同じコミット
  remotes/master #一つ前のコミット
  remotes/new-feature #同じコミット
Enter fullscreen mode Exit fullscreen mode

--no-ffなどのマージオプションは下記が詳しかったです。

  • ローカルのmasterをリモートにpushする
    • git push origin master
> git branch -a
* master  #同じコミット
  new-feature #同じコミット
  remotes/master #同じコミット
  remotes/new-feature #同じコミット
Enter fullscreen mode Exit fullscreen mode

ブランチの削除

Github Flowに則るのであれば、リモートのmasterブランチは常にデプロイ出来る状態なので、この時点でCircleCIなどでビルドが走ってデプロイされるケースがあるかもしれません。リモートとローカルのnew-featureブランチは不要になるので削除しておきましょう。

ローカルのブランチ削除

  • git branch -d new-feature
> git branch -a
* master
  remotes/master
  remotes/new-feature
Enter fullscreen mode Exit fullscreen mode

リモートのブランチ削除

  • git push --delete origin new-feature
> git branch -a
* master
  remotes/master
Enter fullscreen mode Exit fullscreen mode

以上です。実際に業務で使ったわけではないので、おかしな部分があるかもしれません。(特にレビュアーの立場のところは実際にやってないので。。)
おかしな部分があればご指摘ください。

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay