Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undoing a bad pull

Tags:

git

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 writing 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 <[email protected]> Date:   Wed Nov 24 20:38:58 2010 -0500  Merge branch 'master' of github.com:xcompanyx/xRepositoryX  commit db09f0d79d744d6a354142041b47ff5d748999f3 Merge: 81b6c3d... fc73e25... Author: Malvolio <[email protected]> Date:   Wed Nov 24 17:38:16 2010 -0800  Merge branch 'master' of github.com:xcompanyx/xRepositoryX  commit 81b6c3d04b7c464f8750a56282635526a5ef83a1 Author: Michael <[email protected]> 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?

like image 760
Michael Lorton Avatar asked Nov 25 '10 02:11

Michael Lorton


People also ask

Can you undo a pull?

You can use the git reset command to undo a git pull operation. The git reset command resets your repository to a particular point in its history. If you made changes to files before running git pull that you did not commit, those changes will be gone.

How do I revert a pull in git conflict?

On the command line, a simple "git merge --abort" will do this for you. In case you've made a mistake while resolving a conflict and realize this only after completing the merge, you can still easily undo it: just roll back to the commit before the merge happened with "git reset --hard " and start over again.

How do I undo a pull merge?

To undo the merge, use git reset --hard to reset the local repository to a previous state; use git-reflog to find the SHA-1 of the previous state and then reset to it.


2 Answers

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.

like image 165
jtdubs Avatar answered Oct 21 '22 00:10

jtdubs


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.

like image 41
Cascabel Avatar answered Oct 20 '22 23:10

Cascabel