Amend a Git Commit

Philip Wilkinson
Philip WilkinsonSoftware Engineer, Amazon
Published: November 30, 2023

Changing the last commit with --amend

You may want to amend a commit because you forgot to stage a file, mistakes were made in the original commit, or because some small additional changes should be bundled in the last commit rather than a new one. The --amend flag allows you to make changes to the previous commit.

For this, you need to stage the files that you missed or want to modify using git add <file\_name> and then use git commit --amend. A typical workflow for this may take the form:

Bash
$ git add <new_file>
 $ git commit -m “Create new component
 # edit the file to remove unnecessary code
 $ git add <existing_file>
 $ git commit --amend

Staging the <existing\_file> with the second git add allows the git commit --amend functionality to bundle the second set of changes in with those in the last commit.

This allows you to change anything in the previous commit, whether that is removing lines from a file, removing a file, or adding a new file.

When using the --amend flag, Git will open your default text editor to allow you to change the commit message. If you only want to amend files and not the commit message, you can use the --no-edit flag.

Changing the commit message

One way to use the --amend flag is to change the message of the last commit. This can be beneficial when a commit was made too early, a temporary commit message was used, or team conventions weren’t followed in the original message. Changing the commit message can ensure that it accurately describes the previous commit so that others know exactly what the change contains.

To amend the message when no changes are current staged you can run:

Bash
$ git commit --amend

This will then open your chosen text editor to allow you to edit the previous message.

Alternatively, if you don’t want to open the editor, you can simply add the -m flag which will allow you to specify the new message in the same command.

Bash
$ git commit --amend -m “New commit message”

Be careful when amending public commits

git commit --amend works by removing the previous commit and creating a new one. This means that it changes the history of the repository and can make things difficult and messy if the repository has been shared publicly, such as on GitHub, and other developers have built on the previous commit. Therefore it is not recommended to use amend for commits that have already been pushed to a shared repository as it can cause conflicts with other developers' work.

Amending specific commits

git commit --amend is typically used to change the last commit only. Since this command removes the specified commit and creates a new one this affects the commit history and is very likely to cause merge conflicts. If you do want to change specific commits you should instead use:

  • git reset: Which can be used to move the current tip of a branch to a previous commit and thus reset the branch to a previous state
  • git revert: This can be used to “undo” a specific commit by creating a new commit overwriting the old one.
  • git rebase: This can be used to navigate back to a specific commit in your history. You can then used git commit -amend to edit the changes to that specific commit
Written by
Philip Wilkinson
Philip WilkinsonSoftware Engineer, Amazon
Filed under

Related articles


Git Clone, Push, And Pull Over SSH

Learn how to set up an SSH key to clone, push, and pull a Git repository over the SSH protocol.

Undo a Git Rebase

This post will show you how to undo a rebase using git reset, git rebase and git revert

Prompt Show Git Branch In Prompt

Enhance your terminal with a custom Git prompt. Learn different ways to integrate this contextual info, from custom shell functions to Warp context chips and toolkits like Starship and P10K.

Create Folder In GitHub Repository

Learn how to create and push one or more empty directories in a Git repository using `.placeholder` and `README.md` files using both the CLI and the GitHub interface.

Adding a Submodule in Git

This post will show you how to simply add a submodule to a local repository, clone a repository with a submodule, and work within a repository that has a submodule.

Git Push Origin

A breakdown of git push origin

Undo a git push

This post will show you had to simply undo a git push three different ways.

Undo Git Add

Learn how to effectively use 'git add' to stage files in Git for committing, and discover two powerful methods to undo accidental stagings.

How To Remove Secrets From The Git History Remove Secrets From The Git History

Learn how to remove secrets from the Git history using the BFG and git-filter-repo command-line tools.

Git Commit History

Different ways to use Git to understand how to navigate the commit history of a repository or branch

Change Git Origin Remote URL

Learn how to change the remote url of a local git-enabled directory using the git-remote command and Warp's workflow feature.

How To Create a Git Repository

Creating repos in various scenarios with git