Subject: Re: Pushing an --amend-ed commit [and a git-merge-theirs strategy]

From: Paolo Bonzini <[email-masked]>
Date: Mon, 02 Jun 2008 16:55:53 +0200

> 1) Right, changes all done and committed. Push to public repo.
> 2) Bugger, missed out an obvious one-liner in a Makefile. Make change 
> and --amend that last commit.
> 3) Push to public repo again... Ah, "Not a strict subset" error, can't 
> push...
> 
> It's obvious (I think) to me why I get this error - the commit now has a 
> different hash so it looks like it would be the wrong thing to do to 
> allow the push as far as git is concerned. Right?
> 
> So, is it safe to "use the --force" in this instance when pushing? This 
> should just replace the old commit with the --amended commit with no 
> side-effects, shouldn't it?

Yes, but it would screw up other people that pulled from you.

If that's an issue (it most likely is, unless you're just pushing just 
to your own mirror repository), it is better if you do it the other way 
round: adding your commits on top of origin^, starting from the one that 
fixed a typo.  In other words, make the history look like you hadn't 
used --amend.

Do like this: first create git-merge-theirs somewhere in your path; it's 
this three-line script, and it has to have that name.

#! /bin/sh
eval git reset \$$# -- .
git-checkout-index -q -f -a

Don't forget to make it executable. :-)

And here's the magic incantation to be executed on branch master:

git rebase -s theirs --onto origin/master origin/master^ HEAD

Basically, it uses the script created above to place the commits _after_ 
origin/master^ _above_ origin/master.  What the script does is resolve 
conflicts by taking the version in your "master" branch.

To do so, git-merge-theirs uses "git reset" to check out into the index 
the last head passed to it (which we know is the next commit being added 
to the rebase).  Then it extracts the index into the working tree to 
avoid complaints from "git commit" about files not being up-to-date.

There is no builtin git-merge-theirs strategy; if there was one, it 
should make sure that it was only called with two heads, for example.

Anyway, now you can push again.  I suggest however reviewing your commit 
messages and, if necessary, using "git rebase -i origin/master" to edit 
some of them.

Paolo