Live streamJoin us for "State of code review 2024" on July 18thRegister today

How to amend commits with the git amend command

Greg Foster
Greg Foster
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


Amending can be used to alter the most recent commit or even modify older commits in your history.

This guide walks through how to use the git commit command with the --amend flag to edit your commit history.

The git commit --amend command is primarily used to modify the most recent commit. It can change the commit message or incorporate additional changes that were not included in the original commit.

  • Amend the last commit message: If you made a typo or left out crucial information in your last commit message, you can correct it by running git commit --amend. This command opens your default text editor, allowing you to revise the message.

  • Include additional changes: Sometimes, you might forget to add a file or a change to your last commit. You can stage these additional changes with git add and then run git commit --amend to include them in the last commit.

  • Amending without changing the commit message: If you only need to update the commit's content without altering the message, make your changes, stage them with git add and then use git commit --amend --no-edit to add those changes to the most recent commit. This command amends the commit while retaining the original message.

  • Amending a specific commit: To amend a commit that isn't the last one, you'll need to use interactive rebase. Run git rebase -i HEAD~N, replacing N with the number of commits you want to review. Mark the commit you wish to amend with edit, save, and exit. Make your changes, then use git commit --amend followed by git rebase --continue.

Amending a commit message after push:

Assume your commit history looks like this, with three recent commits:

  1. Check the current commit log:

git log --oneline

Output:

Terminal
a3b4c5d (HEAD -> main) Third commit
d4e5f6g Second commit
a1b2c3d First commit

You've realized that you need to amend the "Second commit".

Step 1: Start an interactive rebase:

To modify the "Second commit", you'll start an interactive rebase for the last 3 commits.

git rebase -i HEAD~3

This command opens a text editor with a list of the last 3 commits:

Terminal
pick a1b2c3d First commit
pick d4e5f6g Second commit
pick a3b4c5d Third commit

Step 2: Mark the commit for amending:

In the text editor, change pick to edit for the "Second commit":

Terminal
pick a1b2c3d First commit
edit d4e5f6g Second commit
pick a3b4c5d Third commit

Save and exit the editor. Git will start the rebase and pause at the "Second commit".

Step 3: Amend the commit:

Now, make the necessary changes to your files as required. Once you've made your changes, stage them:

git add .

Next, proceed with the commit amend:

git commit --amend

This opens your editor to amend the commit message. After saving and exiting the editor, the commit is amended.

Step 4: Continue the rebase:

To continue the rebase process, execute:

git rebase --continue

Git attempts to reapply the remaining commits on top of the amended commit.

Console cutput example:

After you run git rebase --continue, you might see:

Applying: Third commit

If there are no conflicts, the rebase completes without further input. However, if there are conflicts, Git will pause and ask you to resolve them.

Step 5: Final verification:

To verify the changes, view the commit log:

git log --oneline

You'll see the updated commit message for the "Second commit", along with the other commits, potentially with new commit IDs due to the rebase:

Terminal
x7y8z9a (HEAD -> main) Third commit
u8v9w0x Amended second commit
a1b2c3d First commit

Notes:

  • Editing Past Commits: Remember, editing past commits with rebase can rewrite Git history. This can be problematic for shared branches. Always coordinate with your team before rewriting history of shared branches.

  • Conflict Resolution: If you encounter conflicts during git rebase --continue, resolve the conflicts in your editor, then use git add to stage the resolved files, and run git rebase --continue again.

  • Force Pushing: After amending commits in a branch that has been pushed to a remote repository, you may need to force push using git push --force. Exercise caution as this replaces the history on the remote and can affect others working on the same branch.

  • Preview Your Changes: Before amending, use git diff and git status to review the changes you're about to commit. This ensures you're amending exactly what you intend to.

  • Understand the implications of force pushing: Force pushing (git push --force) overwrites history on the remote repository. It should be used sparingly and only after confirming that it won't negatively impact your team.

For further reading on amending commits, see the official git documentation.

Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Get started

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2