LoginSignup
13

More than 5 years have passed since last update.

プルリク前にgit reset --softで簡単にコミットをまとめる方法

Posted at

プルリク前にコミットを一つにまとめたい場合、良く有る方法としては新たにブランチを切り直して、
そちらのブランチにgit merge --squashしてコミットをまとめるという方法が取られると思います。

git reset --softの場合は以下の手順でコミットをまとめます。

git reset --soft ${ブランチを切った時点でのコミットハッシュ}
git add .
git commit -m "あらたなコミットメッセージ"

共同開発しているブランチであれば、レビュー用にブランチを新たに切ったほうが良いと思いますが、
個人で開発しているブランチで、自分以外がcheckoutしたりコミットしないブランチであれば、
git reset --softでまとめてしまうのが一番楽かと思います。

※git reset --softはHEADが指し示すコミットを移動させる事が出来ますが、詳しくは以前のエントリーを参照してください。
http://qiita.com/LOUIS_rui/items/8bc0c9058a69a3d6de97

具体例はこんな感じです。

# masterブランチに1コミットし、トピックブランチに2コミットする
$ git init
$ echo initial >> test
$ git add test 
$ git commit -m "initial commit from master"
$ git checkout -b topic master
$ echo second >> test
$ git add test 
$ git commit -m "second commit from topic"
$ echo third >> test
$ git add test 
$ git commit -m "third commit from topic"
# コミットをまとめる前
$ git show-branch --sha1-name
! [master] initial commit from master
 * [topic] third commit from topic
--
 * [809dcec] third commit from topic
 * [fa4af23] second commit from topic
+* [6340706] initial commit from master
$ git reset --soft 6340706
$ cat test 
$ git status
$ git add test 
$ git commit -m "commit from topic"
# 以下の通りコミットはまとめられました
$ git show-branch --sha1-name
! [master] initial commit from master
 * [topic] commit from topic
--
 * [3c57414] commit from topic
+* [6340706] initial commit from master

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
13