git.day/1 • See all Git of the day at git.day

Don't keep creating new commits to fix something small in the last commit on your feature branch. New commits clutter the history and make it harder to review and audit later.

Having created a commit with git commit, the commit can be edited with staged changes. While it's not possible to literally edit commits, this amend makes it easy to make a copy of the commit with changes.

Git graph diagram showing one commit being replaced by another.

Keep commit message

As one would normally do for a new commit, git add any further changes (or use -a to commit all modifications to tracked files), then

git commit --amend --no-editCode language: Shell Session (shell)

The two options on this command are

  • --amend to change the current commit (at HEAD).
  • --no-edit to not edit the commit message. This can be omitted to open your editor to modify the commit message.

The new commit created as part of the amend will have a new hash. Any commits with the old commit as ancestor won't be moved.

Change commit message

While it is common to not wish to edit the commit message when amending for small changes. To edit the commit message, omit --no-edit. Git opens the configured Git editor to allow you to edit the commit message, whether there are any staged changes or not. Save and close the file to continue amending.


Advertisements
Advertisements

Leave a Reply

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