How to change previous commit message and contents

Some­times you just make a com­mit and real­ize that the com­mit mes­sage could have been bet­ter. Or that you for­got to add a file. Maybe you just made a com­mit and then saw that you missed a semi colon in a few places or for­got to delete those extra log­ging statements.

Git lets you eas­ily chance the com­mit mes­sage or the con­tents of the most recent com­mit using the command

git commit --amend

But the trick is to use this com­mand after using the git add com­mand if you need to add mod­i­fied files to the pre­vi­ous commit.

The sequence of oper­a­tions would be

1) Change some files
2) git add .
3) git com­mit –amend

The com­mand opens up edi­tor in which you can edit your com­mit mes­sage as well.

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

Ryan Sukale

Ryan is just a regular guy next door trying to manage his life and finances.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *