Confused about github rebasing

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Mar 15 14:37:09 PDT 2012


On Thu, Mar 15, 2012 at 09:51:30PM +0100, Alex Rønne Petersen wrote:
> On 15-03-2012 21:49, H. S. Teoh wrote:
[...]
> >Another question. How to I repair my current history, which is all
> >messed up now? That is, my branch has a whole bunch of commits I didn't
> >make; how do I clean it up? Or is it easier to start from scratch? :)
> >
> >
> >T
> >
> 
> Well, it really depends on how the history looks... How many commits
> do you have? If it's a small number, just branch off upstream and
> cherry-pick each commit.
[...]

OK, so I finally figured out how to repair it.

Actually, I discovered that my origin/master branch was also broken
(probably due to running the wrong git command in it in the past),
because it had a bunch of commits from upstream that for some reason had
different hashes (maybe the result of attempting to merge from a messed
up branch?). This is probably why my topic branch was messed up in the
first place, I think.

So I did git reset --hard to clean up the history back to the point
where it first starting diverging, then pulled from upstream/master
(which I confirmed were fast-forward). Then I used `git push -f master`
to clean up the history on github. So that takes care of master.

Then in my topic branch, which had a messed up history:

	(branchpoint)---(fast-forwards from upstream) <-- master
	     \
	      \---(merged)---(merged)---...---(my changes) <-- topic

I did a `git rebase --onto master (my changes)`:

	(branchpoint)---(fast-forwards from upstream) <-- master
	    \                                      \
	     \---(now unreachable commits)          \--(my changes) <-- topic

Then I used git push -f to cleanup the history on github.

So now everything looks OK again. Phew!

I did discover an interesting thing about git though... at one point in
the cleanup process I accidentally reset 1 more commit than I had
intended, and that commit had no other references to it (unreachable).
But luckily I still had its hash available, so `git merge <hash>`
managed to restore it. I guess old unreachable commits are still kept
until you run git gc.

So that's one of the times when you *don't* want to run git gc. :-)


T

-- 
Laissez-faire is a French term commonly interpreted by Conservatives to
mean 'lazy fairy,' which is the belief that if governments are lazy
enough, the Good Fairy will come down from heaven and do all their work
for them.


More information about the Digitalmars-d-learn mailing list