views:

11348

answers:

6

When I've worked a bit with my source code, I do my usual thing commit and then I push to a remote repo. But then I noticed I forgot to organize my imports in the source code. So I do the amend command to replace the previous commit:

> git commit --amend

Unfortunately the commit can't be pushed back to the repository. It is rejected like this:

> git push origin
To //my.remote.repo.com/stuff.git/
 ! [rejected]        master -> master (non-fast forward)
error: failed to push some refs to '//my.remote.repo.com/stuff.git/'

What should I do? (I can access the remote repo)

+1  A: 

I had to fix this problem with pulling from the remote repo and deal with the merge conflicts that arose, commit and then push. But I feel like there is a better way.

Spoike
Not really. The issue might be that you haven't updated your local copy from the remote repo. Git won't push to it because you may have to deal with merges manually.In my other reply, I have a command (and explanation) that will force a push -- but beware that is may delete changes in the remote.
mipadi
+19  A: 

Short answer: Don't push amended commits to a public repo.

Long answer: A few Git commands, like git commit --amend and git rebase, actually rewrite the history graph. This is fine as long as you haven't published your changes, but once you do, you really shouldn't be mucking around with the history, because if someone already got your changes, then when they try to pull again, it might fail. Instead of amending a commit, you should just make a new commit with the changes.

However, if you really, really want to push an amended commit, you can do so like this:

$ git push origin +master:master

The leading + sign will force the push to occur, even if it doesn't result in a "fast-forward" commit. (A fast-forward commit occurs when the changes you are pushing are a direct descendant of the changes already in the public repo.)

mipadi
+13  A: 

What you are seeing is a git safety feature. git refuses to update the remote branch with your branch because your branch's head commit is not a direct descendent of the current head commit of the branch that you are pushing to.

If this were not the case, then two people pushing to the same repository at about the same time would not know that there was a new commit coming in at the same time and whoever pushed last would lose the work of the previous pusher without either of them realising this.

If you know that you are the only person pushing and you want to push an amended commit or push a commit that winds back the branch, you can 'force' git to update the remote branch by using the -f switch.

git push -f origin master

Even this may not work as git allows remote repositories to refuse non-fastforward pushes at the far end by using the config variable 'receive.denynonfastforwards'. If this is the case the rejection reason will look like this (note the 'remote rejected' part):

 ! [remote rejected] master -> master (non-fast forward)

To get around this, you either need to change the remote repository's config or as a dirty hack you can delete and recreate the branch thus:

git push origin :master
git push origin master

In general the last parameter to git push uses the format <local_ref>:<remote_ref>, where local_ref is the name of the branch on the local repository and remote_ref is the name of the branch on the remote repository. This command pair uses two shorthands. :master has a null local_ref which means push a null branch to the remote side master, i.e. delete the remote branch. A branch name with no : means push the local branch with the given name to the remote branch with the same name. master in this situation is short for master:master.

Charles Bailey
saved the day, thanks
Eelco
+32  A: 

I actually once pushed with --force to git.git repository and got scolded by Linus BIG TIME. It will create a lot of problems for other people. A simple answer is "don't do it".

I see others gave the recipe for doing so anyway, so I won't repeat them here, but here is a tip to recover from the situation after you have pushed out the amended commit with --force (or +master).

  1. First, you find the old commit that you amended (call it OLD, and we'll call the new commit you created by amending NEW).
  2. Then create a merge between OLD and NEW, recording the tree of NEW, like "git checkout NEW && git merge -s ours OLD".
  3. Merge that to your master with "git merge master"
  4. Update your master with the result with "git push . HEAD:master"
  5. Then push the result out.

Then people who were unfortunate enough to have based their work on the commit you obliterated by amending and forcing a push (which is your being a very very bad boy) will see the resulting merge will see that you favor NEW over OLD. Their later merges will not see the conflicts between OLD and NEW that resulted from your amending and they do not have to suffer.

I'm very well aware what happens when you force push an amended commit (by destroying history). Luckily enough, I was the only developer on the project with the remote repo being on a network drive so it wasn't that big of a deal. I never thought about merging an amend commit, so I'll upvote this.
Spoike
+12  A: 

Quick rant: The fact that no-one has posted the simple answer here demonstrates the desperate user-hostility exhibited by the Git CLI.

Anyway, the "obvious" way to do this, assuming you haven't tried to force the push, is to pull first. This pulls the change that you ammended (and so no longer have) so that you have it again.

Once you have resolved any conflicts, you can push again.

Yes, I wrote about this, see http://stackoverflow.com/questions/253055/how-do-i-push-amended-commit-to-the-remote-git-repo/1459351#1459351 ;)
Spoike
Thanks that was very helpful.
faB
A: 

I usually only have to do this when I have to clean up from committing something that I never should have (a secure file or large binary that I missed/forgot about with .gitignore) then I need to push that out to the central repo, but I too, feel like there should be a better way. I would say never ever ever do this for a normal code change.

coteyr