How do I modify a specific commit?
Categories:
How to Modify a Specific Git Commit

Learn various techniques to alter, amend, or rebase specific commits in your Git history, from the most recent to older, published commits.
Modifying a specific commit in Git is a common task, whether you need to fix a typo, add a forgotten file, or reword a commit message. Git provides powerful tools for rewriting history, but it's crucial to understand the implications, especially when dealing with shared repositories. This article will guide you through different scenarios for modifying commits, from the most recent to older ones, and discuss best practices.
Modifying the Most Recent Commit (HEAD
)
The easiest commit to modify is the very last one you made, often referred to as HEAD
. Git's amend
command is specifically designed for this purpose. It allows you to combine staged changes with the previous commit, or simply alter its message, without creating a new commit in the history.
# To add forgotten files or changes to the last commit:
git add <forgotten_file>
git commit --amend --no-edit
# To change only the commit message of the last commit:
git commit --amend -m "New, improved commit message"
Using git commit --amend
to modify the most recent commit.
--no-edit
flag is useful when you want to add changes to the last commit without altering its message. If you omit -m
and --no-edit
, Git will open your configured editor to allow you to change the commit message.Modifying Older Commits with Interactive Rebase
When you need to modify a commit that isn't the most recent one, git rebase --interactive
is your primary tool. This powerful command allows you to rewrite a sequence of commits, offering options like editing, squashing, reordering, or dropping commits. It's essential to understand that rebasing rewrites history, creating new commit hashes for the modified commits and all subsequent commits.
flowchart TD A[Start Interactive Rebase] --> B{Identify Commit to Modify} B --> C[Run `git rebase -i <commit_hash>^`] C --> D{Editor Opens with To-Do List} D --> E["Change 'pick' to 'edit' for target commit"] E --> F[Save and Close Editor] F --> G{Git Pauses at Target Commit} G --> H[Make Changes (e.g., `git add`, `git commit --amend`)] H --> I[Continue Rebase (`git rebase --continue`)] I --> J{Resolve Conflicts (if any)} J --> K[Rebase Complete] K --> L[Force Push (if remote)]
Workflow for modifying an older commit using interactive rebase.
1. Identify the Target Commit
First, you need to find the hash of the commit just before the one you want to modify. You can use git log
or git log --oneline
to view your commit history. For example, if you want to modify commit C
, you'll rebase from commit B
(its parent).
2. Start Interactive Rebase
Execute git rebase -i <commit_hash>^
. The ^
symbol tells Git to start the rebase from the parent of the specified commit, including the commit itself in the rebase list. For example, git rebase -i HEAD~3
will rebase the last three commits.
3. Mark the Commit for Editing
Your default text editor will open, showing a list of commits. Find the line corresponding to the commit you want to modify and change the word pick
to edit
(or e
). Save and close the editor.
4. Perform Modifications
Git will pause at the specified commit. You are now in a 'detached HEAD' state at that commit. Make your desired changes (e.g., git add <file>
, git rm <file>
, git commit --amend
). If you only want to change the commit message, use git commit --amend
without staging any files.
5. Continue the Rebase
Once you've made your changes, tell Git to continue the rebase process by running git rebase --continue
. Git will then reapply the subsequent commits on top of your modified commit. You might encounter merge conflicts if later commits touch the same lines you modified; resolve them and then git add
the resolved files before running git rebase --continue
again.
6. Force Push (if necessary)
If you've modified commits that have already been pushed to a remote repository, you'll need to force push your changes: git push --force-with-lease origin <branch_name>
. Use --force-with-lease
as it's safer than --force
, as it prevents overwriting work if others have pushed to the same branch in the meantime. Only do this if you are absolutely sure no one else has pulled your old commits.
git rebase
is a powerful operation that changes commit hashes. If you modify commits that have already been pushed to a shared remote repository, it can cause significant problems for collaborators who have based their work on the old history. Always communicate with your team before force pushing, or avoid rebasing shared branches entirely.Changing a Commit Message Only (Older Commits)
If your only goal is to change the commit message of an older commit, interactive rebase is still the way to go, but with a slightly different command in the rebase editor.
# Start interactive rebase, e.g., for the last 3 commits
git rebase -i HEAD~3
# In the editor, change 'pick' to 'reword' (or 'r') for the target commit:
# pick 1a2b3c4 Old commit message
# reword 1a2b3c4 Old commit message
# Save and close the editor. Git will then open another editor for the specific commit message.
# Edit the message, save, and close. The rebase will continue automatically.
Using reword
in interactive rebase to change an older commit message.