pbs-assess / git-course

A repository for teaching the fundamentals of GIT
5 stars 80 forks source link

How to edit a commit message? #9

Closed andrew-edwards closed 7 years ago

andrew-edwards commented 7 years ago

I often do a commit and then realise the commit message could have been clearer (or more correct). I think I looked into this in the past but don't have something that I use - maybe worth looking up. Even if it can't/shouldn't be done, then at least we'd have an answer, as someone is bound to ask (after committing and pushing something).

cgrandin commented 7 years ago

git commit --amend -m "Correct message"

This only works on the last message, you can't change any other commit using amend.

If you pushed already, you can do: git push --force after the amend command to push the change, but it gets more complicated if others have already pulled after your commit.

I was planning on adding this to the slides. I make spelling mistakes quite a lot in commit messages!

cgrandin commented 7 years ago

If the branch is not master and you have already pushed: git commit --amend -m "Correct message" git push --force origin branch-name

andrew-edwards commented 7 years ago

Great, thanks. I expect the 'if others have already pulled' bit might not be too important - I find I often want to just correct the message straight away.

cgrandin commented 7 years ago

Me too. If others have pulled already, or a commit other than the last one has to be changed, you need to use the rebase command and the syntax starts getting quite a bit uglier.