How to amend and update a git commit

Johnny Simpson - Aug 29 '22 - - Dev Community

Have you ever made a commit message with git commit like this?

git commit -m "Fixed CSS"
Enter fullscreen mode Exit fullscreen mode

Only to remember the hundreds of articles you've read on writing "real" commit messages, and to immediately regret your decision? If you've ever done this, you can undo your commit, but an easier way to update your git message is with --amend:

git commit --amend -m "feat-new-ui: Updated margins by 0.25rem"
Enter fullscreen mode Exit fullscreen mode

Now you can easily update your commit messages by simply adding --amend to your git command.

Other uses for git commit --amend

Not only can git commit --amend be used to make changes to a git message, but we can also use it to add files to an already committed change. For example, let's say you forgot to add the file style.css to your commit, but you want it all to exist on the same commit.

All you have to do, is use git add to add the file as you normally would like so, and use git commit --amend --no-edit to add the file to your existing git commit. Simple!

git add style.css
git commit --amend --no-edit
Enter fullscreen mode Exit fullscreen mode

Now your already made commit will have the file style.css included, and the message for that commit will remain the same.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .