Why can't I push an empty commit?
Mia Lopez
git commit --amend --allow-emptythen
git push origin masterthe 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?
04 Answers
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 masterShould you have done:
git commit --allow-emptyYou 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 --amendit 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.
Make sure the remote branch you are trying to push to isn't currently checked out. I made a git repository on one of my servers once and couldn't figure out why I couldn't push to it. After a day or so of troubleshooting, I found out that I couldn't push to the repository (or the branch I wanted to) while it was checked out on the server repository. So, I simply made a new branch that I checkout when I'm done making changes on the server, and I can then push to the server. This might not be your issue, but I was getting an error similar to this when I had an issue pushing to an empty git on my server.