Why can't I push an empty commit?
git commit --amend --allow-empty
then
git push origin master
the git said that
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'remoteurl'
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.
Why? How to fix this?
The issue is not that you are pushing an empty commit.
It is about pushing a different commit (one with a different SHA1) than the one commit already pushed.
That is what git commit --amend
does: it modified the last commit, it doesn't create a new one.
That means you are pushing a different history than the one others might have already cloned.
If you are sure that won't be a problem, you need to force the push:
git push -f origin master
Should you have done:
git commit --allow-empty
You would have created a new (empty) commit, which you could have pushed without any issue.
If you want to create pull request on Github. You can:
git commit --allow-empty -m "make pull request"
Then create pull request with no change.
To clarify the accepted answer, since I don't have enough reputation to comment:
When you use
git commit --amend
it does create a new commit. However, it doesn't append it to the current commit, it appends it to the parent of the current commit. Visually, it would look like a fork.
O (old commit)
/
O-O (amended commit)
Git interprets this as a divergence from the remote. That is why it will not let you push it without forcing.