0

So I accidently made a commit with a comment message that was inappropriate while I was in the middle of a messy merge conflict. Is there a way to change this comment? I have made commits since then that I would rather not undo.

I tried to google it but I couldn't find a way to do it since i've made 9 commits since then. I don't want to undo those commits because some of those are important. Also I made that commit from my server, so do I need to fix it from that server? Thanks.

Makyen
  • 31,849
  • 12
  • 86
  • 121
  • 3
    Possible duplicate of [How to modify existing, unpushed commit messages?](https://stackoverflow.com/questions/179123/how-to-modify-existing-unpushed-commit-messages) – phd Jul 25 '19 at 18:38
  • https://stackoverflow.com/search?q=%5Bgit%5D+edit+commit – phd Jul 25 '19 at 18:38

1 Answers1

2

Disclaimer: rewriting Git history can be dangerous (when the history is public or shared with others, including yourself on a different system). That said, …


Use Git's interactive rebase mechanism, to reword commits:

git rebase -i $some_commit_before_your_commit

This will show you a list of commits prepended with pick each. Change that pick to reword (or simply r) for the commit you want to edit. Save and exit the file to start the rebasing process. Your $EDITOR will automatically open with the commit's message prefilled. Change to your liking, save, exit. Wait for rebase to finish. Done.

If you then try to push this branch, Git will complain about a "non fast-forward". This is due to the fact that history was rewritten. You need to force-push in this case (see disclaimer above).

knittl
  • 246,190
  • 53
  • 318
  • 364