GitHubにコミットしてプッシュした後、コミットメッセージを編集する方法はありますか?「メモを追加」とインラインコメントがありますが、コミットメッセージの実際の編集はありません。git拡張には「コミットの修正」もありますが、既存のメッセージは編集されません。
GitHubにコミットしてプッシュした後、コミットメッセージを編集する方法はありますか?「メモを追加」とインラインコメントがありますが、コミットメッセージの実際の編集はありません。git拡張には「コミットの修正」もありますが、既存のメッセージは編集されません。
回答:
git rebase -i <commit hash you want to change>^
これにより、デフォルトのエディター(通常はvi)が開き、それぞれのコミットとアクションのリストが表示されます。デフォルトでは、アクションはpickです。
メッセージを変更するコミットについては、に変更pickしrewordます。
保存して終了します(vi:で:wq)。
そのようなコミットごとに、コミットメッセージを編集するためのエディターを取得します。必要に応じて変更し、保存して終了します。
すべてのコミットメッセージの編集が完了すると、コマンドプロンプトに戻り、更新されたメッセージを含む新しいツリーが作成されます。
を使用してgithubにアップロードできますgit push origin --force。
最後のコミットを修正する必要がある場合は、手順1〜4をに置き換えることができますgit commit --amend。
^そこにいることに注意してください-変更したいコミットの親に基づいてリベースすることを実際に提案しました。
^^リテラルを使用してコマンドを終了する^ 例: git rebase -i 2c747b32^^
Intellij Ideaでは、とても簡単にできます。
git push origin --force@Mureinikの回答で示唆されているように、後で実行する必要があります。
あなたのgitグラフが次のように見える場合...
O target-commit that you want to change its message [df9c192]
|
O parent-commit [b7ec061]
|
O
(df9c192そしてb7ec061、別々にtarget-commitとparent-commitのコミットハッシュです)
次の指示を入力するだけです...
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)別のオプションは、エラーを含むコミットオブジェクトを参照する追加の「エラータコミット」(およびプッシュ)を作成することです。新しいエラータコミットも修正を提供します。エラッタコミットとは、実質的なコードの変更はなく、重要なコミットメッセージがあるコミットです。たとえば、readmeファイルにスペース文字を1つ追加し、その変更を重要なコミットメッセージでコミットするか、gitオプションを使用します--allow-empty。リベースよりも確かに簡単で安全であり、真の履歴を変更せず、ブランチツリーをクリーンに保ちます(使用amendまた、最新のコミットを修正する場合にも適していますが、古いコミットにはエラータコミットが適している場合があります)。この種のことはめったに起こらないので、単に間違いを文書化するだけで十分です。将来、機能キーワードのgitログを検索する必要がある場合、元のコミット(元のタイプミス)で間違ったキーワードが使用されたため、元の(誤った)コミットは表示されない可能性がありますが、キーワードは表示されますエラッタコミットでは、タイプミスがあった元のコミットが示されます。以下に例を示します。
$ git log
コミット0c28141c68adae276840f17ccd4766542c33cf1d
著者:最初の最後
日付:2018年8月8日水曜日15:55:52 -0600
エラータのコミット:
このコミットには実質的なコード変更はありません。
このコミットは、前のコミットメッセージの修正を文書化するためにのみ提供されています。
これは、オブジェクトe083a7abd8deb5776cb304fa13731a4182a24be1をコミットすることに関するものです。
元の不正なコミットメッセージ:
背景色を赤に変更しました
修正(*強調表示の変更*):
背景色を*青*に変更しました
コミット032d0ff0601bff79bdef3c6f0a02ebfa061c4ad4
著者:最初の最後
日付:2018年8月8日水曜日15:43:16 -0600
暫定的なコミットメッセージ
コミットe083a7abd8deb5776cb304fa13731a4182a24be1
著者:最初の最後
日付:2018年8月8日(水)13:31:32 -0600
背景色を赤に変更しました
@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、などのコマンドのプレフィックスなしでトップにメッセージをコミットします。これはすでに編集ページであり、トップメッセージを直接編集してsave&quit することができます。例:<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からコミットハッシュを確認したり、リポジトリの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行方不明を^した場合、保存せずにエディタを終了し、理由を理解する方が良いでしょう:noop
# Rebase 8db7e8b..fa20af3 onto 8db7e8b
...
noop問題がなければ、単語pickをrewordに変更するだけで、他はそのまま残ります(この時点ではコミットメッセージを編集しないでください)。例えば:reword <commit hash> <your current commit message>
# Rebase 8db7e8b..fa20af3 onto 8db7e8b
#
# Commands:
# p, pick = use commit
...
<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
#
# interactive rebase in progress; onto b057371
# Last command done (1 command done):
# reword d996ffb <existing commit message foo bar>
# No commands remaining.
# You are currently editing a commit while rebasing branch 'master' on 'b057371'.
#
# changes to be committed:
# modified: foo.py
#
<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です。詳細については、ドキュメントを参照してください。