Using git amend for quick corrections

Now that you know how to rewrite your git history with rebase, let’s take a look at amending your commits. An amend is much simpler and, for the case of typos or other minor corrections you discover immediately after a commit, sometimes much more appropriate.

For the setup, we’re going to add some markdown to the 12-days-of-commitmas repository’s, but with incorrect tags. A URL is of the format “[text to display](url://to/visit)”. It’s easy to get this backwards or to use the same tags on both sides of the URL. Here, I’ve used square braces for the URL and commited it anyway. Whoops!

git amend fig 1

Git amend is pretty simple. Just make another edit and add —amend (that’s two hyphens) to your commit command. An editor will open where you can update the commit message. Since this is a typo and the purpose of the commit hasn’t changed, I just hit ZZ to save without editing and git processes the amend command.

git amend fig 2

Just like when rebasing, notice that the commit checksum has changed. If you had already pushed to origin, you would now need to add -f to force overwriting the remote’s commit history.

This is a lot simpler than rebasing. Amending is appropriate when tweaking the previous single commit with changes that are similar to the described purpose of the commit – typos, syntax corrections, steps you left out entirely – and inappropriate when making unrelated changes that deserve their own commit. Rebasing is appropriate when consolidating discreet commits into fewer commits to express a relationship between multiple changes or simply to make the history a little nicer and easier to review. Your judgement, combined with the project’s guidelines, should help determine the correct course of action.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s