Icon
Sreeram Venkitesh
Published on

Easier ways to rewrite history with git

206 words • 2 min read

Whenever I made a git commit and wanted to update the file changes in that specific commit, this is what I usually do:

  • Reset the HEAD
  • Make the necessary changes
  • Stage all the files and recommit

Or if I made a typo and wanted to update the commit message:

  • Reset the HEAD
  • Stage all files
  • Commit again with new message

This is the noob way of doing things.

There is a neater way to achieve both of these with the --amend flag. You can use git commit --amend to update commits in place and "rewrite history".

For updating commit messages, simply run:

git commit --amend

This would bring up the commit message editor and you can update the message and save and exit vim.

For updating file changes, make the file changes you want to add to the latest commit. Stage these changes and run:

git commit --amend --no-edit

This would add all the stage changes to the latest commit.

The --no-edit flag can be used if you want to update the file changes without updating the commit message. If you don't pass it, you can update both the files and the commit message.

Happy committing!