Amend a Git Commit

Last Updated on 2023-03-23

[#change-last-commit]Changing the last commit with [.inline-code]--amend[.inline-code][#change-last-commit]

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 [.inline-code]--amend[.inline-code] 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 [.inline-code]git add <file_name>[.inline-code] and then use [.inline-code]git commit --amend[.inline-code]. A typical workflow for this may take the form:

 $ 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 [.inline-code]git add[.inline-code] allows the [.inline-code]git commit --amend[.inline-code] 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 [.inline-code]--amend[.inline-code] 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 [.inline-code]--no-edit[.inline-code] flag.

[#change-commit-message]Changing the commit message[#change-commit-message]

One way to use the [.inline-code]--amend[.inline-code] 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:

 $ 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 [.inline-code]-m[.inline-code] flag which will allow you to specify the new message in the same command.

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

[#amend-public-commits]Be careful when amending public commits[#amend-public-commits]

[.inline-code]git commit --amend[.inline-code] 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.

[#amend-specific-commits]Amending specific commits[#amend-specific-commits]

[.inline-code]git commit --amend[.inline-code] 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:

  • [.inline-code]git reset[.inline-code]: 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
  • [.inline-code]git revert[.inline-code]: This can be used to “undo” a specific commit by creating a new commit overwriting the old one.
  • [.inline-code]git rebase[.inline-code]: This can be used to navigate back to a specific commit in your history. You can then used [.inline-code]git commit -amend[.inline-code] to edit the changes to that specific commit