This guide will cover several methods to add changes to a previous commit, including using git commit --amend
, interactive rebase, and amending a merge commit. We'll also discuss how to handle scenarios when the changes have already been pushed to a remote repository.
Using git commit --amend
to modify the last commit
If you need to make changes to the most recent commit, to add missing files or update the commit message for example, you can use git commit --amend
. This is the simplest form of revising a commit.
Let's walk through how to use the git commit --amend
command in a few common scenarios.
Adding additional files to the last commit
Stage the additional files you want to include in the last commit:
Terminalgit add <file-path>Replace
<file-path>
with the path to the file you want to add.Amend the last commit
If you want to add the file without changing the commit message, run:
Terminalgit commit --amend --no-editThis command adds the staged changes to the last commit while keeping the same commit message.
Amend the commit message
If you want to add the files and also edit the commit message you can do so by running:
Terminalgit commit --amend -m "New commit message"This command will still add teh staged changes to the last commit, but will also replace the commit message with the new message specified by the
-m
flag.
Interactive Rebase to Edit a Commit in History
If the commit you want to modify is not the most recent one, you can use git rebase
in interactive mode to edit any commit in your project’s history.
Start an interactive rebase for the last
n
commits:Terminalgit rebase -i HEAD~nReplace
n
with the number of commits back from the current HEAD that includes the commit you want to edit.Select the commit you wish to edit Let's assume you have decided to change a commit message and you start the rebase with:
git rebase -i HEAD~3
This command initiates an interactive rebase of the last three commits. After running this, Git opens your configured text editor with a file that lists the recent commits:
pick e3a1b35 Add navigation barpick 97ac9d8 Update background colorpick f30ab87 Fix typo in README
The list of commits you see is in reverse chronological order, meaning the newest commit is at the top. Each line represents a commit, and the word pick
tells Git to take the commit as is.
To amend the commit where you updated the background color, you change the word pick
to edit
next to the corresponding commit:
pick e3a1b35 Add navigation baredit 97ac9d8 Update background colorpick f30ab87 Fix typo in README
After you've made this change, save and close the editor. Git will then reapply the commits up to the one you've marked for editing and pause, allowing you to amend the commit. The terminal will reflect this by returning control to you and showing a message like:
Stopped at 97ac9d8... Update background colorYou can amend the commit now, withgit commit --amendOnce you are satisfied with your changes, rungit rebase --continue
At this point, you can change the commit message or make other amendments as necessary.
Make your changes in the working directory
Create new files or modify existing ones as needed.
Stage the changes for commit by running:
Terminalgit add <file-path>Amend the commit:
Terminalgit commit --amendContinue the rebase:
Terminalgit rebase --continueRepeat this process if you have marked more than one commit for editing.
Dealing with changes after push
If you have already pushed the commit you amended or rebased, you will need to force push the changes.
Once you have completed the steps to amend the commit, to force push your changes, re-writing the history of the commit that was already pushed, run:
git push --force
This can overwrite history on the remote, so it should be done with caution, especially in a collaborative environment.
For further reading on adding changes to a previous Git commit, see the official documentation.