pull request送るまでは適当なコミットメッセージにしておく

CakePHPのCookbook翻訳で、gitの利用方法を以前書きました。
http://d.hatena.ne.jp/cakephper/20120709/1341808861

その時は、本家の最新の状態をrebaseで反映してから自分のリポジトリのブランチにpushするまでの流れを書きました。
その後は、githubのページから自分のブランチのページへ移動して、pullリクエストをcakephp/docsのmasterブランチに投げればマージされるのを待つのみになります。


今回、RESTの章を気が向いた時に少しずつ翻訳しながら進めていました。
https://github.com/cakephp/docs/pull/407

そうすると、毎回作業の終わりにコミットしたくなるのですが、途中経過のファイルをコミットするため、コミットメッセージとかどうしようかなと迷ってました。
いろいろと考えるのが面倒なので、コミットメッセージは適当な文字 1st, 2ndとかにして、最後のpull requestを送る前にrebase -iでコミットを1つにまとめて、そのときにちゃんとしたコミットメッセージにすることにしました。
(注意点としては、その作業ブランチをgithubにpushしないことです。pushすると最後にまとめてしまうコミットと不整合が起こるので)


まずは、自分の作業ブランチに行って、push前に最新の本家masterブランチを取り込んでおき、
下記操作でmasterとの差分コミットを1つのコミットに統合します

git rebase -i master

そうすると、masterには無いこのブランチのコミットが下記のように表示されます

 pick d648a70 1st
 pick a988cd1 2nd

 # Rebase f680aba..a988cd1 onto f680aba
 #
 # Commands:
 #  p, pick = use commit
 #  e, edit = use commit, but stop for amending
 #  s, squash = use commit, but meld into previous commit
 #
 # If you remove a line here THAT COMMIT WILL BE LOST.
 # However, if you remove everything, the rebase will be aborted.

pickはコミットを残す、squashは上のコミットに入れてしまう、という意味なので、下記のように2行目のコミットの先頭をpickからsに変えます

 pick d648a70 1st
 s a988cd1 2nd

そのままエディタを保存終了すると、今度はコミットメッセージの編集画面になるので、ここで正式なコミットメッセージを記述します。

 # This is a combination of 2 commits.
 # The first commit's message is:
 1st

 # This is the 2nd commit message:

 2nd

 # Please enter the commit message for your changes. Lines starting
 # with '#' will be ignored, and an empty message aborts the commit.
 # Not currently on any branch.
 # Changes to be committed:
 #   (use "git reset HEAD <file>..." to unstage)
 #
 #       new file:   ja/development/hoge.txt

#はコミットメッセージから無視されるので、1stと2ndという仮コミットメッセージを消して、
正式なコミットメッセージのみ記載して、エディタを保存終了します。

その後、git log -p などすれば、1コミットにまとまっているのが分かると思います。


参考:「GitHubへpull requestする際のベストプラクティス」 http://d.hatena.ne.jp/hnw/20110528