[Git]コメントの変更

ローカルリポジトリでのコメント変更方法についてまとめる。

直前のコミットを変更する

‘–amend’ オプションで変更する。

$ git commit --amend -m '変更後のコメント'
$ git log --oneline
1111111 (HEAD -> test) 変更後のコメント
2222222 a
3333333 (origin/master, master) first

任意のコメントを修正する

下記のコミットログがあったとする。

$ git log --oneline
0d1240b (HEAD) e
0af6fd9 d
877e98b c
c057698 b
...

この時に2つ前のコミット(877e98b)を変更したい場合は ‘git rebase’ で変更を行う。
「HEADから何番目のコミットか」と言う指定方法もあるが、自分は下記のようにコミットIDを指定して変更している。
コミットIDは「変更したいコミットの直前のコミットID」を指定する。

$ git rebase -i c057698

指定するとエディタが開くので、該当のコミットに対しての操作を指定する。

e 26b9f9c c            <-- ここを指定
pick 177d8e8 d
pick dee68e2 e

# Rebase c057698..dee68e2 onto c057698 (3 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
...

エディタを閉じると下記のメッセージが表示され、 ‘git commit –amend’ で変更できる旨が表示される。

Stopped at 26b9f9c...  c
You can amend the commit now, with

  git commit --amend 

Once you are satisfied with your changes, run

  git rebase --continue

コメントを編集する。

$ git commit --amend -m 'c modified'

リベース処理は完了していない(この時点だと停止している)ので、コメント変更後にリベース処理を再開させる。

$ git rebase --continue

リベースが完了するとコメント変更処理が完了している。

$ git  log --oneline
cf8b47e (HEAD -> test) e
1f71327 d
975324f c modified
c057698 b
...

rebaseでの操作方法

‘git rebase’ コマンド実行後の操作の指定方法はエディタ上のコメントに記載されている。
※()内に日本語で記載。

# Commands:
# p, pick = use commit(コミットを利用する)
# r, reword = use commit, but edit the commit message(コミットを利用するが、コミットメッセージを編集)
# e, edit = use commit, but stop for amending(コミットを利用するが、コミットを修正する(ステージにあるが、git commit されていない状態))
# s, squash = use commit, but meld into previous commit(コミットを利用するが、直前のコミットに統合する)
# f, fixup = like "squash", but discard this commit's log message(squashと似ているが、コミットメッセージを破棄する)
# x, exec = run command (the rest of the line) using shell(この行の末尾に指定したシェルコマンドを実行)
# d, drop = remove commit(コミットを削除する)

参考

Gitのコミットメッセージを後から変更する方法をわかりやすく書いてみた

Git:過去のコミットコメントを修正する方法

カテゴリーGit

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

fifteen + seven =

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください