I need to change an old commit message. Considering that I've made a few other commits afterwards, is there a way to change it, via git or directly on GitHub, without interfering with the other commits?
You can't use git commit --amend
because it's not your most recent commit.
You would want to do a rebase, something similar to
git rebase -i HEAD~3
Where 3 would be how many commits back you'd like to go.
This is doing an interactive rebase. On the screen or text window that opens, replace pick
with reword
.
On the next screen or text window, you will then be able to change the commit message(s).
Doing a rebase changes the commit hashes, so you will need to do a git push --force-with-lease
otherwise your changes will be rejected from the server.
--force-with-lease
is generally safer than --force
when doing potentially destructive commits.
See the Amending older or multiple commit messages from the link @Myffo posted.
Instead of rebasing and force pushing the modified branch, it's possible to replace a commit with a different message without affecting the existing commit hashes.
The syntax looks like this:
git replace --edit <commit>
This opens the editor and displays something like this:
tree 430db025986d2bf8791be16b370ec37a00f6924b
parent 77efdb98a6e021ca81cd96f7c8c05d25c09e0ad4
author John Doe <[email protected]> 1698219601 +0200
committer John Doe <[email protected]> 1698219601 +0200
<initial commit message>
Modify the message and save.
The modified references will then have to be pushed and fetched explicitly with:
git push origin 'refs/replace/*'
git fetch origin 'refs/replace/*:refs/replace/*'
This requires Git 2.1 or higher.
https://git-scm.com/docs/git-replace
git log
and tig
show the modified commit, but GitHub and GitLab don't, so the usefulness of this feature is still limited.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With