Why can't I push an empty commit?

35

9

  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?

user2195

Posted 2011-08-28T07:48:56.340

Reputation:

Answers

57

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.

VonC

Posted 2011-08-28T07:48:56.340

Reputation: 13 292

5

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.

1Rhino

Posted 2011-08-28T07:48:56.340

Reputation: 51

4

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.

Dustin

Posted 2011-08-28T07:48:56.340

Reputation: 41

0

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.

PRaven

Posted 2011-08-28T07:48:56.340

Reputation: 1