コミットして GitHub にプッシュした後にコミットメッセージを編集する方法はありますか?インラインコメントの他に ‘add a note’ という機能があるようですが、実際にコミットメッセージを編集する方法はありません。git extensions には ‘amend commit’ というものがありますが、これは既存のメッセージを編集するものではありません。
回答 (5)
git rebase -i <commit hash you want to change>^メッセージを変更したいコミットについては、
pickをrewordに変更してください。保存して終了(viでは
:wq)。このようなコミットごとに、コミットメッセージを編集するためのエディタが表示されます。適当に変更して、保存して終了します。
これで、
git push origin --forceを使ってgithubにアップロードできるようになりました。
最後のコミットを修正するだけなら、ステップ1~4をgit commit --amendに置き換えることができます。
前提:
git-graph が以下のようになっていれば…
O target-commit that you want to change its message [df9c192]
|
O parent-commit [b7ec061]
|
O
(df9c192 と b7ec061 はそれぞれ target-commit と parent-commit のコミットハッシュです)
解決方法。
以下の命令を入力するだけでOKです。
git reset --soft b7ec061
git commit -m "your_new_description"
git push -f
解説。
git reset --soft b7ec061はファイルの変更を保持し、親コミットにリセットします(例: b7ec061)git commit -m "..."はローカルに新しいコミットを作成するgit push -fは新しいコミットをサーバにプッシュし、古いコミットを置き換えます (例: df9c192)
もうひとつの方法は、エラーを含むコミットオブジェクトを参照する “errata コミット” を追加して (そしてプッシュして) 作成することです。errata コミットとは、実質的なコードの変更はないものの、重要なコミットメッセージを含むコミットのことです。これはリベースよりも簡単で安全ですし、真の履歴を変更することもありませんし、ブランチツリーをきれいに保つことができます (最新のコミットを修正する場合は --allow-empty を使うのもいいでしょうが、古いコミットの場合はエラッタコミットを使うのもいいでしょう)。このようなことは滅多に起こらないので、間違いを文書化するだけで十分です。将来的には、git のログから機能キーワードを探す必要が出てきたときに、元の (誤った) コミットが表示されなくなるかもしれません。以下に例を示します:
$ git log commit 0c28141c68adae276840f17ccd4766542c33cf1d Author: First Last Date: Wed Aug 8 15:55:52 2018 -0600 Errata commit: This commit has no substantive code change. THis commit is provided only to document a correction to a previous commit message. This pertains to commit object e083a7abd8deb5776cb304fa13731a4182a24be1 Original incorrect commit message: Changed background color to red Correction (\*change highlighted\*): Changed background color to \*blue\* commit 032d0ff0601bff79bdef3c6f0a02ebfa061c4ad4 Author: First Last Date: Wed Aug 8 15:43:16 2018 -0600 Some interim commit message commit e083a7abd8deb5776cb304fa13731a4182a24be1 Author: First Last Date: Wed Aug 8 13:31:32 2018 -0600 Changed background color to red
@Mureinikさんの回答は良いが、初心者には理解できない。
最初の方法:
- 最新のコミットメッセージを編集したいだけなら、
git commit --amendだけで済む。
<your existing commit mesage foo bar>
# Please enter the commit message fir your changes. Lines starting
# with # will be ignored, and an empty message aborts the commit.
#
# Date: Sat Aug 24 17:56:16 2019 +0800
#
# On branch is up to date with 'origin/master'.
#
# changes to be committed:
# modified: foo.py
#
- ご覧のように、
pickのようなコマンドの接頭辞を付けずに先頭にあるコミットメッセージは、すでに**編集ページになっており、先頭のメッセージの編集と保存と終了*を指示することができます。
<your new correction commit message>
# Please enter the commit message for your changes. Lines starting
....
- その後、
git push -u origin master --forceまたは<how you push normally> --forceを実行します。ここで重要なのは--forceです。
2番目の方法:
コミットハッシュは
git logで確認するか、リポジトリのURLから抽出します(私の場合は881129d771219cfa29e6f6c2205851a2994a8835次に、
git rebase --interactive 881129d771219cfa29e6f6c2205851a2994a8835またはgit rebase -i HEAD^(最新の場合)このようになります。
pick <commit hash> <your current commit message>
# Rebase 8db7e8b..fa20af3 onto 8db7e8b
#
# 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
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# 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.
#
# Note that empty commits are commented out
- しかし、
noopが表示される場合は、おそらく間違った入力をしている可能性があります。例えば、git rebase -i 881129d771219cfa29e6f6c2205851a2994a88を実行しても最後に^が表示されない場合は、保存せずにエディタを終了して、その理由を考えた方が良いでしょう。 Save&quitすると、メソッド#1と同じような 編集ページ が表示されます:
noop
# Rebase 8db7e8b..fa20af3 onto 8db7e8b
...
- 上部のメッセージを編集し、メソッド#1と同じように save&quitします、例:
reword <commit hash> <your current commit message>
# Rebase 8db7e8b..fa20af3 onto 8db7e8b
#
# Commands:
# p, pick = use commit
...
- ここでもメソッド#1と同じように、
noopまたはpickを実行します。ここで重要なのはrewordです。
詳細は doc を読んでください。