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?
CodePudding user response:
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.
CodePudding user response:
To change an old commit's message you can use either
git rebase -i HEAD~4
(4 is an arbitrary number instead of 4 you have to use the rank of the commit you want to change, most current commit is considered 1)
or
git rebase -i (SHA of the commit you want to edit)^
ex. git rebase -i 993ff4750f38b701383575a95b4efea54cc77658^
(don't forget to add '^' symbol at the end of your sha)
These commands will display the latest commits in your default text editor. In that text editor find the commit you would like to change and replace 'pick' with 'reword'. After you save and exit. Another window will pop up for you to change the the message.
CodePudding user response:
Take a look at this github doc