Thumb

Mastering The Amend Flag

git, version control

2 months ago · 3 min read

Have you ever made a commit, only to realize you forgot to include some files, mistyped a message, or wished you could make a quick edit without adding a new commit? This is where the git commit --amend command shines. The --amend option allows you to modify the last commit, saving you from creating a new one just to fix a minor mistake.

In this post, we’ll explore how to use the amend flag, its benefits, and when to use it (and when not to).

Why Use The Amend Flag?

When working with Git, commits serve as important snapshots of your project. But what if your commit message has a typo, or you forgot to add a crucial file? Rather than cluttering your commit history with fixes like “forgot this file” or “typo correction,” you can amend the last commit to make it cleaner and more cohesive.

Common use cases:

How to Use The Amend Flag

Using the amend flag is straightforward. Let’s go through a few scenarios.

1. Amending a Commit Message

If you only want to change the last commit message, use:

git commit --amend -m "New commit message"

This command opens the editor (or directly updates the message if you use the -m flag) where you can edit your commit message without affecting any code changes.

2. Adding New Changes to the Last Commit

If you have uncommitted changes that should be part of the last commit, stage those changes with git add, then run:

git commit --amend -m "New commit message"

Important Considerations

While the amend flag is useful, there are some things to keep in mind:

When Not to Use The Amend Flag

If your commit has already been pushed to a shared branch, amending it can complicate the history for others who’ve pulled the commit. In these cases, it’s better to create a new commit with corrections.