On 15-03-2012 22:37, H. S. Teoh wrote:
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


See also git reflog. It's a life-saver.

--
- Alex

Reply via email to