To change the message of your most recent commit, run git commit --amend -m "New message". For an older commit, run git rebase -i HEAD~3, change pick to reword on that commit's line, and save; if the commit was already pushed, finish with git push --force-with-lease.
git commit --amend -m "Fix login redirect when session expires"
Change the last commit message (not pushed yet)
The one-line version replaces the message directly:
git commit --amend -m "New message"
Without -m, Git opens your editor with the current message already filled in, which is easier when you only need to fix a typo or the message has several lines:
git commit --amend
Example run (Git 2.45, in a test repository):
$ git log --oneline -1
b56399c Add c
$ git commit --amend -m "Add c (amended)"
[main 08a3702] Add c (amended)
Date: Mon Oct 26 13:30:20 2026 +0200
1 file changed, 1 insertion(+)
create mode 100644 c
The commit ID changed from b56399c to 08a3702. That is expected: --amend doesn't edit the old commit, it creates a replacement. The Git documentation describes it as "Replace the tip of the current branch by creating a new commit." The author and the original author date stay the same.
Watch out for staged changes
Amend builds the new commit from your current staging area. If you have changes staged with git add, they go into the amended commit along with the new message. Run git status first. If something is staged that shouldn't be, unstage it with git restore --staged <file> before amending. (The flip side is useful: staging a forgotten file and running git commit --amend --no-edit adds it to the last commit without touching the message.)
Change the last commit message after pushing
Amend the same way, then push. A normal push is rejected because the remote still has the old commit and your branch no longer contains it (example output, remote URL anonymised):
$ git push
To github.com:alex/project.git
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to 'github.com:alex/project.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. If you want to integrate the remote changes,
hint: use 'git pull' before pushing again.
Don't follow that hint here. git pull would merge the old commit back in, and you'd end up with both versions. Overwrite the remote instead:
git push --force-with-lease
+ 50c87c9...e5064a1 main -> main (forced update)
Use --force-with-lease, not --force. The lease version only overwrites the remote branch if it still points at the commit you last fetched. If a teammate pushed something in the meantime, the push fails instead of silently deleting their work. The git-push docs explain that "blindly pushing with --force will lose their work", while the lease means "the remote ref is updated only if the 'lease' is still valid".
Force-pushing rewrites shared history. On your own feature branch that's routine. On main or any branch other people pull from, everyone who already has the old commit will see their branch diverge, so agree on it first, or leave the message alone.
Change an older commit message with interactive rebase
To reword a commit that isn't the latest one, start an interactive rebase that goes back far enough to include it. HEAD~3 means "the last three commits":
git rebase -i HEAD~3
Git opens a to-do list in your editor, oldest commit first (example, trimmed):
pick ab4631d Add a
pick ff940e1 Add b with typo teh
pick b56399c Add c
# Rebase 3f2e1d0..b56399c onto 3f2e1d0 (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
...
Change pick to reword (or just r) on each commit you want to rename, and leave everything else alone:
pick ab4631d Add a
reword ff940e1 Add b with typo teh
pick b56399c Add c
Save and close the file. Git replays the commits and opens the editor once for each reword line, showing that commit's message. Edit it, save, close. Git prints:
Successfully rebased and updated refs/heads/main.
Check the result with git log --oneline. The reworded commit and every commit after it now have new IDs, because each commit stores its parent's ID. Commits before it keep theirs.
A few details:
- Don't change the commit text on the
picklines in the to-do list. Git ignores it. Only the command word matters; the new message is typed in the editor that opens afterwards. - Don't delete lines. The to-do list warns: "If you remove a line here THAT COMMIT WILL BE LOST."
- To reword the very first commit in the repository, use
git rebase -i --root. - To back out partway through, run
git rebase --abort, which resets the branch to where it was before you started. - If the commits were pushed, finish with
git push --force-with-lease, exactly as for an amended commit.
Rebasing commits that include merges is more involved; git merge vs rebase covers what rebase does to history and why it matters on shared branches.
Pick an editor if Git opens Vim
Both git commit --amend and git rebase -i open Git's configured editor, which is Vim on many systems. If you're stuck in Vim, press Esc, type :wq and press Enter to save and quit, or :q! to quit without saving your edits. To use another editor from now on:
git config --global core.editor "code --wait" # VS Code
git config --global core.editor "nano" # nano
git config --global core.editor "notepad" # Windows Notepad
The --wait flag makes the code command wait until you close the file, so Git doesn't read the message before you have written it.
Which command to use
| Situation | Command | Force push needed? |
|---|---|---|
| Last commit, not pushed | git commit --amend -m "..." | No |
| Last commit, pushed | git commit --amend -m "...", then git push --force-with-lease | Yes |
| Older commit or several commits | git rebase -i HEAD~N, mark them reword | Only if pushed |
| Very first commit in the repo | git rebase -i --root | Only if pushed |
| Pushed to a shared branch you can't rewrite | Leave it; explain in the next commit or the pull request | No |
If the old message contained something sensitive
Rewriting the message locally and force-pushing replaces the commit on the branch, but the old commit can survive elsewhere: in other people's clones, in forks, and on the hosting service. GitHub's documentation warns that force-pushing may not remove the original commit from GitHub and says to contact GitHub Support with the old commit ID to have it purged. If the message held a password or token, treat the secret as leaked and rotate it; editing history does not undo exposure.
If you wanted to undo the commit itself rather than reword it, see how to undo the last Git commit.
Frequently asked questions
- Does changing a commit message change the commit hash?
- Yes. The message is part of the data Git hashes, so any change produces a new commit with a new ID. With git rebase -i, every commit after the reworded one also gets a new ID, because each commit records the ID of its parent.
- Can I change a commit message on GitHub in the browser?
- GitHub's own guide does it locally: change the message with git commit --amend (or git rebase -i for older commits), then replace the commit on GitHub with git push --force-with-lease origin <branch>. The push is what updates the commit shown on GitHub.
- How do I change the author as well as the message?
- Run git commit --amend --reset-author to make the commit's author you (using your current user.name and user.email), which also renews the author timestamp. To set a specific author, use git commit --amend --author="Name <[email protected]>".
- I amended by mistake. How do I get the old commit back?
- Run git reflog. The commit from before the amend appears as HEAD@{1}. git reset --soft HEAD@{1} moves the branch back to it while keeping your files as they are now.