直近のコミットメッセージの書き換え
git commit --amend コマンドで、直近のコミットメッセージを変更できます。
In Git, the text of the commit message is part of the commit. Changing the commit message will change the commit ID--i.e., the SHA1 checksum that names the commit. Effectively, you are creating a new commit that replaces the old one.
Commit has not been pushed online
If the commit only exists in your local repository and has not been pushed to your GitHub Enterprise Server instance, you can amend the commit message with the git commit --amend command.
- コマンドラインで、修正したいコミットのあるリポジトリに移動します。
git commit --amendと入力し、Enter を押します。- テキストエディタでコミットメッセージを編集し、コミットを保存します。
- コミットにトレーラーを追加することで、共作者を追加できます。 詳しい情報については、「複数の作者を持つコミットを作成する」を参照してください。
次回のプッシュ時に、your GitHub Enterprise Server instanceに新たなコミットとメッセージが表示されます。
You can change the default text editor for Git by changing the core.editor setting. For more information, see "Basic Client Configuration" in the Git manual.
Amending older or multiple commit messages
If you have already pushed the commit to your GitHub Enterprise Server instance, you will have to force push a commit with an amended message.
We strongly discourage force pushing, since this changes the history of your repository. If you force push, people who have already cloned your repository will have to manually fix their local history. For more information, see "Recovering from upstream rebase" in the Git manual.
Changing the message of the most recently pushed commit
- 上記の手順に従って、コミットメッセージを修正します。
push --forceコマンドにより、古いコミットをフォースプッシュで上書きします。$ git push --force example-branch
Changing the message of older or multiple commit messages
If you need to amend the message for multiple commits or an older commit, you can use interactive rebase, then force push to change the commit history.
-
コマンドラインで、修正したいコミットのあるリポジトリに移動します。
-
git rebase -i HEAD~nコマンドで、デフォルトのテキストエディタに直近nコミットの一覧を表示できます。# 現在のブランチの最後の 3 つのコミットのリストを表示する $ git rebase -i HEAD~3リストは、以下のようになります。
pick e499d89 Delete CNAME pick 0c39034 Better README pick f7fde4a Change the commit message but push the same commit. # Rebase 9fdb3bd..f7fde4a onto 9fdb3bd # # 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 # # 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 -
変更する各コミットメッセージの前の
pickをrewordに置き換えます。pick e499d89 Delete CNAME reword 0c39034 Better README reword f7fde4a Change the commit message but push the same commit. -
コミット一覧のファイルを保存して閉じます。
-
生成された各コミットコミットファイルに、新しいコミットメッセージを入力し、ファイルを保存して閉じます。
-
When you're ready to push your changes to GitHub, use the push --force command to force push over the old commit.
$ git push --force example-branch
For more information on interactive rebase, see "Interactive mode" in the Git manual.
As before, amending the commit message will result in a new commit with a new ID. However, in this case, every commit that follows the amended commit will also get a new ID because each commit also contains the id of its parent.
If you have included sensitive information in a commit message, force pushing a commit with an amended commit may not remove the original commit from GitHub Enterprise. The old commit will not be a part of a subsequent clone; however, it may still be cached on GitHub Enterprise and accessible via the commit ID. You must contact your GitHub Enterprise site administrator with the old commit ID to have it purged from the remote repository.