• 0

How to change previous commit message and contents

Sometimes you just make a commit and realize that the commit message could have been better. Or that you forgot to add a file. Maybe you just made a commit and then saw that you missed a semi colon in a few places or forgot to delete those extra logging statements.

Git lets you easily chance the commit message or the contents of the most recent commit using the command

git commit --amend

But the trick is to use this command after using the git add command if you need to add modified files to the previous commit.

The sequence of operations would be

1) Change some files
2) git add .
3) git commit –amend

The command opens up editor in which you can edit your commit message as well.

That said, you should only do this if you havent pushed your last commit to your remote. Its a good strategy to wait for a while after your last commit before you push to your remote coz you never know.