Last Updated: January 3, 2026
Editing commits can be a powerful tool in your Git workflow. While reordering commits allows you to organize your commit history more effectively, editing commits takes this a step further. It enables you to modify the content of existing commits, which can be invaluable for correcting mistakes, improving commit messages, or even altering the files included in a commit.
Whether you realize it or not, the commits in your repository tell a story about your development process. Making sure that story is clear and accurate is essential for both your future self and others who may work on the project. In this chapter, we'll explore how to edit commits using Git, the implications of doing so, and some practical examples to solidify your understanding.
A commit in Git is essentially a snapshot of your entire project at a specific point in time. When you edit a commit, you are changing that snapshot. The commit history is a linear sequence of these snapshots, which means that when you change one, you potentially affect all subsequent commits.
Editing commits typically involves two main aspects:
It's important to note that editing commits rewrites history. This is safe when working locally, but if you have already pushed commits to a shared repository, you'll need to be cautious. The changes will create new commit hashes, which can lead to complications for anyone else who has pulled those commits.
git commit --amend CommandThe primary command for editing the latest commit is git commit --amend. This command allows you to modify the most recent commit without creating a new one, effectively replacing it.
To use git commit --amend, first make your changes to the relevant files. Then stage those changes:
Now, amend the commit:
This will open your default text editor with the previous commit message. You can modify the commit message as needed, or leave it as is. When you save and close the editor, Git will update the latest commit to include your new changes.
Imagine you committed a change that introduced a bug. After realizing the mistake, you fix the issue and want to update your last commit. Here’s how you would do it:
Amend the last commit:
Edit the commit message to reflect the fix:
Save and exit the editor.
This approach creates a cleaner commit history by combining the bug fix with the original commit, showing that the change was made as part of that commit's intent.
While git commit --amend is great for the most recent commit, you might find yourself needing to edit an earlier commit. In such cases, you’ll use interactive rebasing to go back to the desired commit and make your edits.
To initiate an interactive rebase, use:
Replace N with the number of commits you want to go back. This command will open a text editor listing the last N commits.
In the editor, you'll see a list of commits prefixed by the word pick. To edit a commit, change pick to edit for the commit you want to modify. For example:
After saving the changes, Git will pause at the commit you chose to edit. You can now modify files or change the commit message:
Make your changes. For example, to modify fixed_file.py:
Amend the commit:
Once you've made your changes, you need to continue the rebase process:
Git will then apply any remaining commits on top of your edited commit. This method allows you to edit multiple commits sequentially, ensuring a clear and organized commit history.
Editing commits alters your project's history, and while it can be beneficial, it’s essential to be aware of its implications.
Editing commits is generally safe in a local context where no one else has pulled from your branch. However, if you've pushed the commits to a shared repository, you'll need to force-push your changes:
Force-pushing can overwrite changes in the remote repository. Be sure to communicate with your team if you must do this.
Each commit in Git has a unique hash. When you edit a commit, it creates a new hash for the updated commit. This can lead to confusion if others have based their work on the original commit. Always consider the potential impact on collaborators before rewriting history.
To make the most of editing commits, here are some best practices to follow:
git commit --amend for small adjustments shortly after the initial commit. This keeps your history clean.Now that you understand how to edit commits, you are ready to explore splitting commits in the next chapter.
We'll look at how to break a single commit into multiple commits, allowing you to enhance the granularity of your commit history while keeping your project organized and clear.