LoginSignup
5
4

More than 5 years have passed since last update.

[WIP]gitで空のbranchをpushするまでを自動化

Last updated at Posted at 2014-12-04

更新が多くなった+新しいコマンドを追加したので別記事にまとめ直しました。
http://qiita.com/KeitaMoromizato/items/23474a40d1a19994dc81


最近WIP PR(Work In Progress Pull Request)による開発を始めたけどプルリクエストを作るまでが面倒なので自動化してみた。

まずはaliasを作るためにconfigに追加

.git/config
[alias "create"]
    pr = "!f(){ .git/Github/create_pullrequest $1;};f"

上記aliasを叩いた時に実行されるコマンドを定義

.git/Github/create_pullrequest
git checkout -b $1
git commit --allow-empty -m "create PullRequest"
git push origin $1
git branch -u origin/$1 $1

コマンドに実行権限を付与

$ chmod a+x .git/Github/create_pullrequest

これで以下のコマンドを叩けば、カレントブランチから新しいブランチを作ってpushまでは自動でできる。

$ git create.pr feature/test

create.prとか言いつつPullRequestを作成するところまでは自動化していないのでそれはまた次の機会に。

===
追記

hubコマンドを使ってPullRequestまで自動化できます。
hubコマンドを使えない方はhomebrewでインストール(Windowsの方ごめんなさい)

$ brew install hub

上で作ったファイルに1行目、6行目を追記。

.git/Github/create_pullrequest
current=`git rev-parse --abbrev-ref HEAD`
git checkout -b $1
git commit --allow-empty -m "create PullRequest"
git push origin $1
git branch -u origin/$1 $1
hub pull-request -m "$1" -b $current

これでPullRequestの作成まで自動化出来ます。

===
追記2

チーム開発をするときは上記スクリプトを生成するスクリプトを書きましょう。以下のスクリプトをレポジトリのどこかに置いて、cloneしたときにgitのルートディレクトリ(.gitがある場所)でスクリプトを実行します。

setup.sh
#!/bin/sh

echo "setup PullRequest..."

mkdir .git/Github
#create PullRequest script
cat << "EOF" > .git/Github/create_pullrequest
#!/bin/sh

current=`git rev-parse --abbrev-ref HEAD`
git checkout -b $1
git commit --allow-empty -m "create PullRequest"
git push origin $1
git branch -u origin/$1 $1
hub pull-request -m "$1" -b $current
EOF

chmod +x .git/Github/create_pullrequest

#setup gitconfig
cat << "EOF" >> .git/config
[alias "create"]
  pr = "!f(){ .git/Github/create_pullrequest $1;};f"
EOF
5
4
0

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
5
4