bigmac-jp blog

web開発関連のメモ

Git メモ2 コミットをまとめる

git rebase -iコマンドで複数のコミットをまとめる。

コミットの単位を細かくしている場合に、git logで履歴を確認するときに、logの粒度が細かすぎて履歴が見にくいことがあります。

例えば、「ユーザ一覧画面のCSV出力機能追加」の対応があったする。

開発時のローカル環境でのコミットの粒度は下記とする。(※適当です)
csvの出力処理
②ユーザ一覧画面にcsv出力ボタンを追加&レイアウト調整
csvの出力項目の変更対応
csv出力バグ対応

上記のコミットを"ユーザ一覧画面CSV出力機能追加"にまとめることにする。

コミットをまとめる前の状態を確認

$ git log
commit e3dd7083961d9c6dcddfs928893f0a2349a89jkdil1jkl (HEAD -> develop)
Author: aisutabetai <xxxxxxx@xxxxxxx.co.jp>
Date:   Thu May 31 15:13:29 2019 +0900

    csv出力バグ対応

commit 1378f176539a90f75sfaiiiowjkdls9978cd115db7b25a
Author: aisutabetai <xxxxxxx@xxxxxxx.co.jp>
Date:   Thu May 29 15:13:29 2019 +0900

    csvの出力項目の変更対応

commit 4325f07f68030b3422a2f1f092ffde651fsafddd111
Author: aisutabetai <xxxxxxx@xxxxxxx.co.jp>
Date:   Thu May 28 15:13:29 2019 +0900

    ユーザ一覧画面にcsv出力ボタンを追加&レイアウト調整

commit e51cbfjka899028j98jfklsa9f1aecdf7b883fec9b4fb5
Author: aisutabetai <xxxxxxx@xxxxxxx.co.jp>
Date:   Thu May 27 15:13:29 2019 +0900

    csvの出力処理

commit a199d24aa76f8f32fafjkla321jkl9kkljkdkslslslse (master)
Author: aisutabetai <xxxxxxx@xxxxxxx.co.jp>
Date:   Thu May 27 15:13:29 2019 +0900

    Initial commit

コミットをまとめる。

$ git rebase -i HEAD~4
#4つ分のコミットをまとめるの"HEAD~4"を指定。"~~~~"でも可能らしい。
#エディターが開く

pick e51cbae csvの出力処理
pick 5706e9a ユーザ一覧画面にcsv出力ボタンを追加&レイアウト調整
pick 1378f17 csvの出力項目の変更対応
pick e3dd708 csv出力バグ対応

# Rebase a199d24..e3dd708 onto a199d24 (4 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
~

4つ分のコミットが表示されている。
各コミットの頭に"pick"が表示されているので、"s"または"squash"に変更して保存。

pick e51cbae csvの出力処理
s 5706e9a ユーザ一覧画面にcsv出力ボタンを追加&レイアウト調整
s 1378f17 csvの出力項目の変更対応
s e3dd708 csv出力バグ対応

# Rebase a199d24..e3dd708 onto a199d24 (4 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out


コミットをまとめたので、コミットメッセージもまとめる。

# This is a combination of 4 commits.
# This is the 1st commit message:

csvの出力処理

# This is the commit message #2:

ユーザ一覧画面にcsv出力ボタンを追加&レイアウト調整

# This is the commit message #3:

csvの出力項目の変更対応

# This is the commit message #4:

csv出力バグ対応

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Thu May 30 15:10:14 2019 +0900
#
# interactive rebase in progress; onto a199d24
# Last commands done (4 commands done):
#    squash 1378f17 csvの出力項目の変更対応
#    squash e3dd708 csv出力バグ対応
# No commands remaining.
# You are currently rebasing branch 'develop' on 'a199d24'.
#
# Changes to be committed:
#       new file:   csvOutput.php
#       new file:   userList.html
#


コミットのメッセージを修正&保存。

# This is a combination of 4 commits.
xxxxxxx-csv出力機能対応
・csvの出力処理
・ユーザ一覧画面にcsv出力ボタンを追加&レイアウト調整
・csvの出力項目の変更対応
・csv出力バグ対応

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Thu May 30 15:10:14 2019 +0900
#
# interactive rebase in progress; onto a199d24
# Last commands done (4 commands done):
#    squash 1378f17 csvの出力項目の変更対応
#    squash e3dd708 csv出力バグ対応
# No commands remaining.
# You are currently rebasing branch 'develop' on 'a199d24'.
#
# Changes to be committed:
#       new file:   csvOutput.php
#       new file:   userList.html
#
~


ログの状況を確認。1つのコミットにまとめられていることを確認。

$ git log
commit da7c22dd619b82c652c8895b78e845f571aac943 (HEAD -> develop)
Author: morita <moritary@capa.co.jp>
Date:   Thu May 30 15:10:14 2019 +0900

    xxxxxxx-csv出力機能対応
    ・csvの出力処理
    ・ユーザ一覧画面にcsv出力ボタンを追加&レイアウト調整
    ・csvの出力項目の変更対応
    ・csv出力バグ対応

commit a199d24aa76f8f3223c7b454d5401668053f4fee (master)
Author: morita <moritary@capa.co.jp>
Date:   Thu May 30 15:08:00 2019 +0900

    Initial commit