How to undo git pull from unlinked project?

I just did something dumb. In my fooclient project, I just did:

git remote add official https://github.com/fooserver
git pull official master

      

In other words, I pulled a completely different codebase (server, not client) into my working directory. Unsurprisingly, there weren't many merge conflicts (filenames are all different after all). Also unsurprisingly, Git did not fully warn me that the repo did not have a single common ancestor.

In this particular situation, I can recover by doing the following:

cp file-i-worked-on.js ~
git reset --hard HEAD # to discard broken merge conflicts
git checkout a12345 # where a12345 is the latest head of fooclient that I had checked out
cp ~/file-i-worked-on.js .

      

But what will be the broader strategy?

+3


source to share


4 answers


This will reset for the last state master:

git reset --hard HEAD@{1}

      



This is possible due to the reflog . Running git reflog

will show you the reflog and allow you to verify that you are back to the correct state.

+4


source


After removing the unwanted remote ( git remote rm ...

) just git reset --hard

to where you want your current branch to be included.

git reset --hard origin/master

      



If you were on before master

, this resets yours master

to the same location as your original remote master. What is it!

0


source


git stash
git reset --hard HEAD^
git stash apply

      

0


source


From this moment

git fetch

      

Check what you got in gitk or git log or whatever. Now merge or replace what you got with where you want.

0


source







All Articles