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
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.
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.
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.
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.
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.
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.
Undo a Git Rebase
This post will show you how to undo a rebase using git reset, git rebase and git revert
Git Push Origin
A breakdown of git push origin
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 Push Tags
This post will show you how to push a single tag, multiple tags, all tags, and tags with commits.
Undoing Git Commits
Explore ways to undo a commit, including git reset, git checkout, and git revert with git while preserving commit history.