1

Is there a way I can update the commit message for the last commit I just made to a local branch?

Reason is: when I push, the remote has a policy that mandates every commit message start with a certain incantation, failing which it rejects the commit.

This is a rather large commit with hours of work and lots of changes. Just wondering if I can avoid having to do a --hard reset and redo my changes?

Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
  • 2
    Possible duplicate of [How to modify existing, unpushed commits?](https://stackoverflow.com/questions/179123/how-to-modify-existing-unpushed-commits) – phd Sep 01 '17 at 15:08

2 Answers2

8

You can easily update the previous commit message. Just say:

git commit --amend

You'll first want to make sure you have no changes staged, otherwise they will be added to the commit. To do that, check that git diff shows no changes, or consult git status. Or say git reset to unstage any changes you may have.

If your editor is correctly configured in Git, it will appear so that you can edit the commit message interactively (the old message will load in the editor first). If you don't have an editor set, for example if you're on a *nix machine and want to use Emacs:

VISUAL=emacs git commit --amend

Often you'd get vi by default.

If you aren't sure which commit you are about to amend, just say:

git show
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
3

Try git commit --amend -m <new message>