Using ‘–amend’ option with git commit (with examples)

Updated: January 27, 2024 By: Guest Contributor Post a comment

Introduction

One of the most common tasks when working with version control systems is the need to make changes to the last commit. Git, a distributed version control system, offers a powerful command for this purpose: git commit --amend. This tutorial will guide you through this option, from basic to advanced usage, with numerous examples.

Prerequisites

  • A basic understanding of Git and command-line usage
  • A Git repository to practice the commands

Why Use git commit --amend?

Before jumping into examples, you should understand when and why to use git commit --amend. This command is handy when you want to make modifications to your most recent commit. It allows you to:

  • Correct any mistakes in your commit message
  • Add forgotten files or changes to the commit
  • Combine staged changes with the previous commit without creating a new commit

Simple Amend Usage

Let’s start with the basic use case: modifying the last commit message.

git commit --amend -m "Updated commit message"

After executing this command, Git will replace the most recent commit with a new commit that contains the updated message.

Amending with Changes

If you have forgotten to include some changes in your last commit, you can stage them as usual and then run the --amend option:

git add missed-file.txt
git commit --amend --no-edit

The --no-edit flag tells Git to keep the original commit message. It is particularly useful when you are only adding changes and don’t want to update the commit message.

Advanced Amend Usage

Moving toward more advanced scenarios, sometimes you might want to amend a commit and edit its message without specifying it in the command line. Running git commit --amend without the -m flag will open the default text editor for modifying the commit message.

git commit --amend

Change the message in the editor, then save and close the file to apply the amendments.

Changing Multiple Commits with Interactive Rebase

While git commit --amend is limited to the last commit, you can change earlier commits by combining an interactive rebase with amend. Start by checking the last few commits:

git log --oneline

Pick the commit you want to modify and perform an interactive rebase:

git rebase -i HEAD~3 # Replace 3 with the number of commits you want to review

In the interactive rebase file, change ‘pick’ to ‘edit’ for the commits you wish to amend, save, and exit. Git will stop at each commit you marked for amending, allowing you to make changes:

git add some-file.txt
git commit --amend --no-edit

After you are done with each commit, continue the rebase with:

git rebase --continue

Repeat the amend process for each commit you marked until the rebase completes.

Avoiding Common Pitfalls

While git commit --amend is powerful, it also comes with some potential pitfalls, especially concerning shared history. When you amend a commit, Git creates a new commit with a different SHA1 hash expression. If you have already pushed the original commit to a shared repository, you should refrain from amending it, as this will cause a mismatch in history.

If you must amend a commit that has been pushed to a shared repository, you will have to force push with git push --force. However, communicating with your team before doing this is crucial to avoid disrupting their workflow.

Conclusion

In this tutorial, we have explored the git commit --amend command and seen several examples that illustrate its basic and advanced uses. We also analyzed the best practices and potential pitfalls associated with changing Git history. As always, use this powerful feature thoughtfully, especially when working with shared repositories.