目の前に僕らの道がある

勉強会とか、技術的にはまったことのメモ

git rebase -i でコミットの歴史を改竄する

前に神速さんが発表していた内容を試してみる。

コマンドは以下の通り。この場合、HEADから数えて3つのコミットを改竄します。

git rebase -i 'HEAD~3'

コマンドをたたくとエディタが起動します。変更したいコミットに対してコマンドを書き換えます。

pick 4b35af7 repeat print
pick 346cfb2 trivial fix
pick 2c9a304 use Perl6::Say

# Rebase a490aef..2c9a304 onto a490aef
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

意味的には以下の通りでしょうか。試してみた限りこんな動きをしてました。

  • pick
    • コミットはそのまま
  • reword
    • コミットメッセージを書き換える
  • edit
    • コミット内容を変更する
  • squash
    • 前のコミットに当該コミットを含める。コミットメッセージは書き換えられる
  • fixup
    • 前のコミットに当該コミットを含める。当該コミットメッセージは破棄する

squashにするともう一度エディタが起動するので、コミットメッセージを書き換えます。

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

repeat print

# This is the 2nd commit message:

trivial fix

# 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)
#
#>--modified:   hoge.pl
#

こんなところでしょうか。
当たり前ですが、他人に渡したことのあるコミットや他人が行ったコミットに対しては歴史改竄を行ってはいけません。