💟

[Git] コミットをまとめる

に公開

概要

今日は帰ろう、ってなった時一旦コミットしたく無いですか❓❓
私は次のようによくやります。

git commit -m "一旦コミット"

結果として"一旦コミット"だらけになるので、それらのコミットを一つにまとめて、ちゃんとしたコミットメッセージに直しています。

メリット

  • 見る側が見やすい。レビューしてもらう立場ならするべきだと思う。
  • 一旦コミットする事の安心感

やり方

HEAD後ろのニョロの数で数を指定している。
コミットを4つまとめてみる。

git rebase -i HEAD~~~~

このコマンドを叩くと下のような画面になるので
まとめられるコミットのコマンドを"pick"から"s"に変更する。
下の説明にあるように、このコマンドは 前のコミットに併合 する操作を行う。
"f"でも同じような操作が可能だが、今回は"s"で進める。

pick cb0cff7 wadu
pick 324669f commit1
pick c1a954c commit2
pick 8e54e96 commit3

# Rebase 1920cfb..8e54e96 onto 1920cfb (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 [-C | -c] <commit> = like "squash" but keep only the previous
#                    commit's log message, unless -C is used, in which case
#                    keep only this commit's message; -c is same as -C but
#                    opens the editor
# 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
# u, update-ref <ref> = track a placeholder for the <ref> to be updated
#                       to this position in the new commits. The <ref> is
#                       updated at the end of the rebase
#
# 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.

一番上が一番古いコミットだから、
それに対して後のコミットを併合させるには下のように
"s"("squash")に変える。

pick cb0cff7 wadu
s 324669f commit1
s c1a954c commit2
s 8e54e96 commit3

すると、以下のようなコミットメッセージの編集画面となる。

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

wadu

# This is the commit message #2:

commit1

# This is the commit message #3:

commit2

# This is the commit message #4:

commit3

# Please enter the commit message for your changes. Lines starting

 ~~以下省略~~

"#"から始まる部分はgitが色々説明してくれてる情報で、全て無視される。
仮にこのままだと、コミットメッセージは以下のようになる。

wadu

commit1

commit2

commit3

下のように修正すればコミットメッセージは"コミットメッセージまとめてみた"となる。

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

コミットメッセージまとめてみた

# This is the commit message #2:



# This is the commit message #3:


# This is the commit message #4:


その他

ミスしてもリベース途中であれば、下のコマンドによりリベースを始める前の状態に戻れる。

git rebase --abort

注意点

  • リモートにプッシュしたあとはこの操作は行わない方がいい。他の作業してる方のツリーにも影響があるから。
    コミットを綺麗にしてからプッシュするようにしましょう。

  • 最新のコミットに全部まとめたいと考えて次のようにするとエラーになる。
    このコマンドは前のコミットに対して併合させるコマンドのため、
    このリストの一番古いコミットに対して、
    前のコミットが無いと出来ませんと怒られる。
    そのため、一つにまとめるのであれば、一番古いコミットにまとめる必要がある。

s cb0cff7 wadu
s 324669f commit1
s c1a954c commit2
pick 8e54e96 commit3
error: cannot 'squash' without a previous commit

Discussion