Gitのコミットメッセージを後から修正変更する方法!

Gitのコミットをした後にコミットメッセージを修正したいと考えたことはないでしょうか?実はGitのコミット後のコメントは後からでも修正することができます。この記事ではGitのコミットメッセージを後から修正する方法を解説しています。

コンテンツ [表示]

  1. 1Gitのコミットメッセージを後から修正変更したい
  2. 2Gitのコミットメッセージを後から修正変更する方法
  3. 2.1直前のコミットメッセージを修正する
  4. 2.22つ以上前のコミットメッセージを修正する

Gitのコミットメッセージを後から修正変更したい

Gitのコミットをしたけど、コミットメッセージが間違っていた!間違えたコミットメッセージを修正したい!と思ったことはないでしょうか?

実はGitのコミットメッセージは後からでも修正することができます。

この記事ではコミット後のコメントからプッシュ後のコメントまで修正する方法を解説していきます。

Gitのコミットメッセージを後から修正変更する方法

それでは早速Gitのコミットメッセージを後から修正する方法を紹介します。

直前のコミットメッセージを修正する

まずは直前のコミットメッセージを修正する方法を紹介します。

コメントを修正するにはgit commit --amendコマンドを実行します。

Gitのコメントを後から修正

git commit --amend -m "修正後のコミットメッセージ"

-mオプションの後に修正後のコミットメッセージを記述します。

これでローカルのコミットメッセージは修正することが出来ました。ですが、リモートのコミットメッセージは修正前のままです。

そこで下記のコマンドを実行することでリモートのコミットメッセージも修正後のコミットメッセージを反映させます。

git push --force origin master

そのままpushするとコミットメッセージを修正したことにより競合が発生してpushに失敗します。--forceオプションを指定することで強制的にpushを行います。

ただ、--forceオプションを使用する際には注意してください。コミットメッセージ以外の部分で競合を起こしている場合でも強制的にローカルリポジトリの内容を上書きするので、リモートリポジトリの内容が削除されてしまいます。とても恐ろしいコマンドなので十分気を付けた上で使用してください。

2つ以上前のコミットメッセージを修正する

次に直前のコミットメッセージだけでなく2つ以上前のコミットメッセージを修正する方法を紹介します。

2つ以上前のコミットメッセージを修正する場合、いくつかのステップを踏む必要があります。

まずはgit rebase -iコマンドを実行する。

git rebase -i HEAD~n

nで数字を指定することで、nつ前までのコミットの一覧をデフォルトのテキストエディタで表示してくれます。

一覧は古いコミットを一番上にして古い順に並んでいます。nつ前のコミットが必然的に一番上に表示されるので、コミットメッセージを修正したものをnで指定することをオススメします。

編集前

pick 1e39814 2回目のコミット
pick e0f5fd0 3回目のコミット

# Rebase 6a205a2..e0f5fd0 onto 6a205a2 (2 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.
#

修正したいコミットのものをnで指定したかと思いますので、一番上のコミットのpickeditに編集します。編集後は保存してエディタを閉じましょう。

編集後

edit 1e39814 2回目のコミット
pick e0f5fd0 3回目のコミット

# Rebase 6a205a2..e0f5fd0 onto 6a205a2 (2 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.
#

再びgit commit --amendコマンドで修正後のコメントを入力します。

git commit --amend -m "修正後のコミットメッセージ"

最後に下記のコマンドを実行することで、ローカルのコミットメッセージの修正が完了です。

git rebase --continue

リモートリポジトリに変更内容を反映させるには先ほどと同じgit push --forceコマンドを使用します。十分に気を付けて使用してください。

git push --force origin master
GeekHive採用サイト

関連記事