LoginSignup
12

More than 5 years have passed since last update.

Githubでブランチ作成からプルリク作成までを自動化する

Posted at

githubにリポジトリをホスティングしている開発プロジェクトの場合のよくある開発手順

  1. masterからfeatureブランチを派生 git checkout -b feature/hoge
  2. featureブランチに空コミット git commit --allow-empty -m "Make pull-request [ci skip]"
  3. githubにプッシュ git push -u origin feature/hoge
  4. ブラウザでgithubを開く
  5. プルリク作成

これをコマンド1発で作成できるようにしました。

参考記事

GitHub の Pull Request の作成/終了に付随する作業を alias を使って楽にする | Developers.IO

hubのインストール

hubはgithubが提供しているコマンドラインツールです。
このツールを使ってコマンドラインからプルリクを作れるようにします。
インストールはbrewから

$ brew install hub

hubを使ってプルリクを作るには

$ hub pull-request 

https://github.com/example/example/pull/23

これでプルリクが作られました。作成後に表示されるURLがプルリクのURLになります。

.gitconfigにエイリアス追加

gitconfigに手順をひとまとめにしたエイリアスmkprを作成します。

.gitconfig

[alias]
    ec = commit --allow-empty -m \"Make pull-request [ci skip]\"
    mkpr = !"f() { git checkout -b $1; git ec; git push -u origin $1; open $(hub pull-request -m "$1"); }; f"

ecは空コミットのエイリアスです。 \"Make pull-request [ci skip]\"のコミットメッセージ部分は自由に変更してください。
mkprはブランチ作成, 空コミット, プッシュ, プルリク作成を順に実行しています。最後のopenはmacの標準ブラウザを開くコマンドで、hub pull-requestが返すプルリクのURLをブラウザで開きます。

$ git mkpr feature/hoge

Switched to a new branch 'feature/hoge'
[feature/hoge 47c410a] Make pull-request [ci skip]
Counting objects: 1, done.
Writing objects: 100% (1/1), 196 bytes | 196.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To github.com:example/example.git
 * [new branch]      feature/hoge -> feature/hoge
Branch feature/hoge set up to track remote branch feature/hoge from origin.

参考記事とはmkprのコマンドの中身を若干変えて、引数で渡した文字列feature/hogeをタイトルにしてプルリクを作成し、ブラウザから開くようにしています。
作成後はサマリは空状態なので、適宜ブラウザからタイトルとサマリを編集しましょう。

もっと楽にしたい

GithubにはIssueとプルリクのテンプレート機能があるので、hubコマンドからプルリク作成した場合もこのテンプレートファイルを使ってサマリ部分を記述済みにしたい。

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
12