TBJ TBJ - 11 months ago 67
Git Question

How to undo git pull with error message

I just did a mistake and would really need to undo a git pull command. The mistake was that I wrote

git pull origin devbranch
in stead of
git pull origin master
. So i fetched the wrong branch into my master branch on my site. I am lucky that the merge didn't go through though.

I now get this message when I do
git status

On branch master
Your branch and 'origin/master' have diverged,
and have 10 and 1 different commit each, respectively.
(use "git pull" to merge the remote branch into yours)
All conflicts fixed but you are still merging.
(use "git commit" to conclude merge)

Changes to be committed:

modified: sites/all/modules/mymodule/mymodule.module

Untracked files:
(use "git add <file>..." to include in what will be committed)



I know
git pull
consist of
git fetch
git merge
, and in this cas the merge is not completed, only the fetch part.

How do I go back to the state before my disastrous "
git pull
". I have already used git reflog to see what reference the former pull command used and then I did git
reset --hard a0d3fe6
(where a0d3fe6 is the reference from the previous pull). This didn't change anything. I still get the same message after
git status
. What to do?

Answer Source

If this happens again remember that in a merge, git only adds commits. If the merge had completed, all the original commits are still there. So you can recover the previous state of your branch with

git reset --hard HEAD@{1}

to see the commits that HEAD was on, (which is the list 'HEAD@{1}' uses):

git reflog -32

and at any time view the commits you have with

git log --oneline --decorate --graph -32 --branch --tags --remotes

also if you are going to do something complex like a rebase you can tag the starting commit with

git tag MyStartingCommit 

and then if things are all fouled up just restart the current branch with:

git reset --hard MyStartingCommit