开发者

Undoing a bad pull

First of all, Git sucks. I know, I know, it's supposed to be the best thing since sliced bread, but it sucks. It's like trying to shave with a chainsaw: one slight mistake and there are blood and teeth everywhere. Perhaps, if I could actually make an exact distinction between a head, a ref, a commit, a branch, a stem, a gnargel, and a whizpoo, some of this might be a little easier but to an ordinary mortal with only 10 years experience of SVN, Perforce, and RCS, it all looks like rather cranky black magic.

Now for some reason, git pull has never worked for me. I get a 10-line error message that has been, so far, about as helpful as the word "error". Googling the error message produced a wide array of suggestions that only had in common the fact that they had no apparent affect whatsoever. But that's not today's problem: I've gotten used to typing git pull origin branch.

Today, I was flipping back and forth between two branches, "master" and "lounge" and at the moment I was in the master branch. I wanted to get the latest changes from the remote repository to the local one, but I mistyped. Instead of w开发者_StackOverflow中文版riting git pull origin master, I wrote git pull origin lounge and then, without thinking, typed in the correct command.

There's no evidence of the first (bad) pull in log, just two merges from the master:

commit 0c6be9569bab0581244ea8603bf2edfee82cdd7b
Merge: 43fdec5... db09f0d...
Author: Malvolio <info@xcompanyx.com>
Date:   Wed Nov 24 20:38:58 2010 -0500

Merge branch 'master' of github.com:xcompanyx/xRepositoryX

commit db09f0d79d744d6a354142041b47ff5d748999f3
Merge: 81b6c3d... fc73e25...
Author: Malvolio <info@xcompanyx.com>
Date:   Wed Nov 24 17:38:16 2010 -0800

Merge branch 'master' of github.com:xcompanyx/xRepositoryX

commit 81b6c3d04b7c464f8750a56282635526a5ef83a1
Author: Michael <info@xcompanyx.com>
Date:   Wed Nov 24 17:38:07 2010 -0800
    the last commit I did

But files newly created in the lounge branch are there in my repository.

So now I'm fscked, right? Should I just torch my repository, clone the remote again, reapply all the unpushed changes manually, and chalk it up to Git sucking or is there some incantation I can recite that will make it all better? Would it help if I sacrificed a goat?


Use git reflog to see what your HEAD pointed to before you screwed it up.

You should see something like:

48ab8d HEAD@{0}: pull: Fast-forward
a34bda HEAD@{5}: commit: my last commit message

Now, point your master branch back at the commit before the bad pull:

git reset --hard a34bda

Done. Like it never happened.


I can't totally understand your history from your question, but in general, if you make two merges (a pull is a merge), it is completely straightforward to remove one of them.

- o - o - o - A - M1 - M2 (master)
                 /    /
      - o - o - o    /   (origin/lounge)
                    /
             - o - o (origin/master)

The most obvious way:

 git checkout master
 git reset --hard A
 git merge origin/master

This uses your locally cached version of origin's master, if you want to merge whatever's out there now, use git pull origin master.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜