More Git answers

by Danny
Published 3 min read

Fix a typo or wrong message in a pushed commit with git commit --amend or interactive rebase, then update the remote safely with --force-with-lease.

The short answer

#

For the most recent commit, run git commit --amend to rewrite the message, then force push with a lease. For an older commit, use interactive rebase and mark it as reword. Only do this on a branch nobody else is building on.


_10
git commit --amend -m "Fix login redirect on expired sessions"
_10
git push --force-with-lease

Why it happens

#

A commit's message is part of the commit itself, and the commit's hash is calculated from its contents, including the message. You cannot edit a commit in place. Changing the message creates a brand new commit with a new hash, and the branch is moved to point at it.

Locally that is harmless. The remote, though, still has the old commit. When you push, Git sees that your branch no longer contains the remote's latest commit and rejects the push as non-fast-forward. You have to tell it to replace the remote branch, which is what a force push does.

How to fix it

#

The last commit

Make sure nothing is staged that you do not want added, then amend:


_10
git status # nothing staged, or only things you want in this commit
_10
git commit --amend # opens your editor with the current message
_10
# or set it directly
_10
git commit --amend -m "New message"

--amend also adds anything that is staged into the commit. If you only want to change the message, check git status first, or add --only to ignore the staging area.

An older commit

Start an interactive rebase that includes the commit. To reach the third commit back:


_10
git rebase -i HEAD~3

In the editor, change pick to reword (or r) on the line you want, save and close. Git replays the commits and stops to let you write the new message.


_10
pick a1b2c3d Add search box
_10
reword d4e5f6a Fix serach results order
_10
pick 0a1b2c3 Add pagination

Every commit after the reworded one also gets a new hash, because each commit records its parent.

Push the rewritten branch


_10
git push --force-with-lease

--force-with-lease only overwrites the remote branch if it still points where you last saw it. If a teammate pushed in the meantime, the push fails instead of silently deleting their work. Plain --force has no such check. Run git fetch and look at what arrived before trying again.

When the branch is shared

If other people have pulled the branch, rewriting it means their copies contain commits that no longer exist on the remote. Their next pull will try to merge the old and new versions together and can duplicate commits. Before rewriting a shared branch:

  • Tell the people who use it.
  • Ask them to push anything outstanding first.
  • Afterwards, they can update with git fetch followed by git reset --hard origin/branch-name if they have no local work, or git rebase origin/branch-name if they do.

On main, or on any protected branch, do not rewrite history at all. Most hosts block force pushes to protected branches anyway. Live with the message, or add a follow-up commit or a note in the pull request.

If it is part of an open pull request

Rewording and force pushing a pull request branch is normal and well supported. The pull request updates to show the new commits. If your team squash merges, you can often just edit the final squash commit message in the merge dialog instead.

Common pitfalls

#
  • Amending with files staged and accidentally adding them to the commit.
  • Using git push --force and wiping out a teammate's commits. Prefer --force-with-lease.
  • Rewriting commits on main or a release branch that others depend on.
  • Expecting the old commit to disappear completely. Anyone who fetched it, and any CI system that built it, still has it. If the message contained a secret, rotate the secret; rewriting history does not make it safe.
  • Rewording a commit that is a merge commit with a plain interactive rebase. By default rebase drops merges; use --rebase-merges if you need to keep them.
#

Was this resource helpful?