1

I followed the steps from Change old commit message on Git, and I confirmed the commit message updated in git log. But when I click the "Commit History" tab in Github, I still see the old message and old SHA-1.

So, I blindly tried git push origin master, but that failed with:

To git@github.com:foobar.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:foobar.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.

The error tells me to "merge with remote changes before pushing again", but that seems like the wrong thing to do: merging new stuff into an amended commit (from an older base). I'm confused.

How can I commit my amended message?

Community
  • 1
  • 1

2 Answers2

3

if you are rewriting history, you need to call:

git push --force
plus-
  • 45,453
  • 15
  • 60
  • 73
1

As @xsace says, you are rewriting history here.

A commit in git is linked to several things:

  • the commit metadata (author, committer, timestamps, commit message);
  • the parent commit (or commit_s_ if it is a merge);
  • the tree.

If you change any of these, and you did here (you changed the commit message), your history and github's (or any other remote's for that matter) diverge:

[parent]----[master@github]
    \
     \------[your local master]

Here, you want to push yourlocalmaster to github, but this is not a fast forward, ie the current master head at github is not an immediate parent to your commit. Therefore you need to force the push.

This can also be done with git push github +master.

fge
  • 119,121
  • 33
  • 254
  • 329