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.
Here’s the updated section for your guide, introducing the Graphite CLI and its gt modify
command for amending commits:
Using the Graphite CLI to amend a commit
The Graphite CLI simplifies complex Git workflows, making it easier to manage changes in your branches. One of its standout features is the gt modify
command, which allows you to amend a commit seamlessly while ensuring that stacked branches remain consistent. Here’s how you can use gt modify
to amend a commit:
Amending the last commit with Graphite CLI
If you need to make changes to the most recent commit in your stack (e.g., adding new files or fixing a typo), the gt modify
command offers a streamlined solution.
Stage your changes:
Terminalecho "some updates" >> file.jsAmend the commit with
gt modify
: To automatically stage all changes and amend the last commit, run:Terminalgt modify --allThis command will update the last commit in your branch and ensure that any branches stacked on top are automatically restacked.
Adding a commit message: If you prefer to add a new message to your amended commit, use:
Terminalgt modify --all --message "Updated commit with additional changes"Visualizing your stack: After amending the commit, you can check the updated stack with:
Terminalgt log short
Restacking branches after an amendment
One of the unique features of the Graphite CLI is its ability to automatically restack dependent branches after an amendment. For example, if you amend a commit in a bottom branch of a stack, Graphite will rebase the subsequent branches to maintain consistency.
Benefits of using gt modify
over traditional methods
- Efficiency: You don’t need to manually rebase branches after an amendment.
- Integration: The Graphite CLI’s stacking model ensures that PRs remain consistent even as changes propagate through a stack.
- Flexibility: Easily amend commits with or without creating additional changes.
Using Graphite CLI, you can avoid the complexity of managing Git commands directly while improving your workflow efficiency. Whether amending a single commit or handling changes across multiple branches, gt modify
is a powerful tool to streamline your development process.
For more information about Graphite CLI, visit the official documentation.