• Modern UX

    Edit and navigate faster in the terminal with Warp's IDE-like input editor.

  • Warp AI

    AI suggests what commands to run and learns from your documentation.

  • Agent Mode

    Delegate tasks to AI and use natural language on the command line.

  • Warp Drive

    Save and share interactive notebooks, workflows, and environment variables.

  • All Features

Amend a Git Commit

Thumbnail for Philip WilkinsonPhilip Wilkinson

Philip Wilkinson

Software Engineer, Amazon

Published: 11/30/2023

About Terminus

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:

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

Run in Warp

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:

 $ git commit --amend

Run in Warp

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.

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

Run in Warp

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

Thumbnail for Philip WilkinsonPhilip Wilkinson

Philip Wilkinson

Software Engineer, Amazon

Filed Under

Related Articles

Undo A Git Pull

How to effectively remove the commits introduced by a pull in Git using git-reset and preserve your local changes using git-stash. Also, how to cancel an unmerged pull request on GitHub.

Git
Thumbnail for Glory KimGlory Kim

Undo a Git Merge

How to rollback the changes introduced by a merge in Git by adding new opposite commits using git-revert and effectively removing commits using git-reset.

Git
Thumbnail for Philip WilkinsonPhilip Wilkinson

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.

Git
Thumbnail for Gabriel ManricksGabriel Manricks

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
Thumbnail for Utsav PoudelUtsav Poudel

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
Thumbnail for Philip WilkinsonPhilip Wilkinson

Undo a git push

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

Git
Thumbnail for Philip WilkinsonPhilip Wilkinson

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.

Git
Thumbnail for Glory KimGlory Kim

Undo a Git Rebase

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

Git
Thumbnail for Philip WilkinsonPhilip Wilkinson

Git Push Origin

A breakdown of git push origin

Git
Thumbnail for Amanda KhooAmanda Khoo

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.

Git
Thumbnail for Razvan LudosanuRazvan Ludosanu

Git Push Tags

This post will show you how to push a single tag, multiple tags, all tags, and tags with commits.

Git
Thumbnail for Philip WilkinsonPhilip Wilkinson

Undoing Git Commits

Explore ways to undo a commit, including git reset, git checkout, and git revert with git while preserving commit history.

Git
Thumbnail for Philip WilkinsonPhilip Wilkinson

Trusted by hundreds of thousands of professional developers

Download Warp to get started

Download for Mac
Request demo
Thumbnail for null