0
0
個人開発エンジニア応援 - 個人開発の成果や知見を共有しよう!-

過去の黒歴史(gitのコミットメッセージ)を修正する方法

Last updated at Posted at 2023-10-01

はじめに

急いでいてgitのコメントを適当にしてしまったりして、あとから修正したい...ってなりますよね。
過去の黒歴史(gitのコメント)は消せます!!!
Sourcetreeからでも消せます!!!(*'▽')

記事の要旨

最終コミットのコメントの修正git Bash/Sourcetree
2回以上前のコミットのコメントの修正git Bash/Sourcetree

環境

git Bash
$ git --version
git version 2.41.0.windows.3

既定のエディタはVScodeを設定しています。

最終コミットのコメント修正

画像中の「ccc」というコミットコメントを「cccを追加」に変更しようと思います。
image.png


1.git Bashから変更

手順

  1. リポジトリに移動し、git checkout <ブランチ名>でブランチをチェックアウト
  2. git commit --amendを実行
  3. COMMIT_EDITMSGでコミットメッセージを編集し、閉じる
  4. コミットメッセージの編集が完了。git Bash上にログが出力

解説

gitのローカルリポジトリ(.gitファイルが存在する場所)に移動し、コメントを編集したいブランチにcheckoutします。下の例では既にmasterにcheckoutしているので「Already on 'master'」と表示されます。

git Bash
username@XXXXXX ~/Documents/develop (master)
$ git checkout master
Already on 'master'

git commit --amendを実行します。するとgit Bashは既定のエディタでCOMMIT_EDITMSGを開こうとします。私の場合はVScodeが起動します。

git Bash
$ git commit --amend
hint: Waiting for your editor to close the file...

画像の「ccc」の部分を「cccを追加」に書き換えて保存します。その後、COMMIT_EDITMSGを閉じます。
ここで行頭に#をつけると無視されます。

COMMIT_EDITMSG
ccc

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Sun Oct 1 17:29:18 2023 +0900
#
# On branch master
# Changes to be committed:
#	modified:   test.txt
#

COMMIT_EDITMSGを閉じて間もなく以下のように表示され、コミット時のコメントが書き換わりました。

git Bash
username@XXXXXX ~/Documents/develop (master)
$ git commit --amend
[master 3a9c59c] cccを追加
 Date: Sun Oct 1 17:29:18 2023 +0900
 1 file changed, 2 insertions(+), 1 deletion(-)


2.Sourcetreeから変更

手順

  1. コミットを選択
  2. オプションのコミットを選択
  3. コミットメッセージを編集し、閉じる。
  4. コミットメッセージの編集が完了。

解説

コミットを選択します。
image.png

続いてオプションのコミットを選択します。
image.png

最後のコミットである「ccc」が表示されるので、「cccを追加」に書き換えてコミットすれば完了です。
image.png

2回以上前のコミットのコメント修正

さて、最後のコミット以外の修正はgit amendではできません。が、rebaseを用いることで修正できます。今回はaaaというコミットを修正していきます。

image.png

3.git Bashから変更

手順

  1. コミットのハッシュを特定する(git log --oneline
  2. git rebase -i <編集したいコミットより下のコミットのハッシュ値>を実行する
  3. git-rebase-todoがエディタで開く
  4. git-rebase-todoで、編集したいコミットコメントのpickrewordに変更し、git-rebase-todoを閉じる
  5. rewordの各コミットメッセージについて、COMMIT_EDITMSGがエディタで開くのでコミットメッセージを編集する
  6. 全ての編集を終えたらコミットメッセージの編集が完了。

解説

まず、修正したいコミットのハッシュを特定する必要があります。ハッシュとは、コミットごとに割り振られる名前とかマイナンバーと考えてよいでしょう(笑)
どのコミットを修正するかを指定するにはこのハッシュで指定します。

コミット履歴を表示し、該当するコミットのハッシュを探します。そのために「git log --oneline」を実行してコミット履歴を表示します。

git Bash
username@XXXXXX ~/Documents/develop (master)
$ git log --oneline
7eee4a1 (HEAD -> master) cccを追加
5189359 bbbを追加
42ac209 aaa
142d90c testを作成

さてさて、「git log --oneline」を実行すると今までのコミットメッセージが並んでいますね。
コミットの左側に表示される文字列がコミットのハッシュです。
例えば、「aaa」というコメントのハッシュは「42ac209」です。
つまりgitは、「aaa」のコメントが設定されたコミットを「42ac209」という名称で認識していることになります。

git rebase -i <編集したいコミットより下のコミットのハッシュ値>を実行します。

ここで、なぜ編集したいコミットより下のコミットのハッシュ値を指定するのか?
雑に言うとrebaseというのが、あるコミットに対して、それよりも上の(子にあたる)コミットを編集して乗っけなおすという処理だからです。なので、編集したいコミットよりも下のコミットを指定して実行します。

今回の例では、「aaa」のコミットのハッシュ値「42ac209」ではなく、その下の「testを作成」のハッシュ値である「142d90c」を指定します。

git Bash
$ git rebase -i 142d90c
hint: Waiting for your editor to close the file...

するとgit Bashは既定のエディタでgit-rebase-todoを開こうとします。私の場合はVScodeが起動します。

git-rebase-todo
pick 42ac209 aaa
pick 5189359 bbbを追加
pick 7eee4a1 cccを追加

# Rebase 142d90c..7eee4a1 onto 142d90c (3 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.
#

さて、この画面のコメント行を見てください。ここに各コマンドの意味が記載されています。
デフォルトでは各コミットについてpickが指定されています。代表的なものは以下になります。

コマンド 意味
pick そのコミットを使用する
reword コミットメッセージを編集してそのコミットを使用する
drop コミットを削除します

今回は「aaa」のコミットメッセージの編集が目的なので、「pick 42ac209 aaa」を「reword 42ac209 aaa」に変更します。

git-rebase-todo
reword 42ac209 aaa
pick 5189359 bbbを追加
pick 7eee4a1 cccを追加

# Rebase 142d90c..7eee4a1 onto 142d90c (3 commands)
#(以下略)

git-rebase-todoを閉じると各コミットについてCOMMIT_EDITMSGがエディタで起動します。ここでコメントを編集してCOMMIT_EDITMSGを閉じればコミットメッセージの変更ができます。

COMMIT_EDITMSG
aaa

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# (以下略)
COMMIT_EDITMSG
aaaを追加した

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# (以下略)

エディタを閉じたら、Git Bashまたはターミナルに戻り、しばらくすると "update Successfully" と表示されるはずです。これで無事コミットメッセージの編集が完了。

git Bash
$ git rebase -i 142d90c
[detached HEAD a32c5a8] aaaを追加した
 Date: Sun Oct 1 16:48:42 2023 +0900
 1 file changed, 1 insertion(+)
Successfully rebased and updated refs/heads/master.

image.png

4.Sourcetreeから変更

手順

  1. 編集したいコミットの1つ下のコミットを選択し、右クリックする
  2. <コミットのハッシュ>の子とインタラクティブなリベースを行うを選択する
  3. 編集したいコミットを選択して、メッセージを編集を選択する
  4. メッセージを編集し、OKを2回選択してポップアップを閉じる

解説

編集したいコミットの1つ下のコミットを選択し、右クリックをします。
なぜ編集したいコミットの1つ下のコミットを選択するのかは、「git Bashを使う方法の2」を参照してください。
メニューの中から、<コミットのハッシュ>の子とインタラクティブなリベースを行うを選択します。

image.png

以下のような画面が表示されるので、編集したいコミットを選択して、メッセージを編集を選択します。

image.png

メッセージを編集し、OKを2回選択してポップアップを閉じると、コメントが無事書き換わっています。
image.png

トラブルシューティング

※ rebaseがうまくいかない場合は以下のコマンドを試してみてください。(git Bash)
git rebase --continue
エラーなどにより停止しているrebase処理を再度実行する。一度コマンドラインでCtrl+Cを押して処理を終了し、実行してみてください。保留しているrebaseが存在するかどうかはgit statusで確認できます。

git rebase --abort
rebase処理を停止する。このコマンド実行後、再度git rebase -i <コミットのハッシュ値>を実行してください。

最後に

gitの最終コメントを直す方法はよく見つかるんですが、ほかのコメントを直す方法は見つかりにくい+分かりづらいなあと思ってました。また、Sourcetreeからでも直せること、覚えておくと便利かなと思います!何気に初投稿でした。ではでは~(^^♪

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0