local changes are lost when using git

Issue

I am using Bitbucket for versioning control. Unfortunately, I pushed my commit to wrong repository then I got

fatal: refusing to merge unrelated histories

then I applied this command before I realize it is the wrong repo.

git pull origin master --allow-unrelated-histories

now my project is missed up and I also lost my local changes which I do not have backup for.
is there any way I can restore my local changes and discard the remote origin.

Solution

First thing is, if those changes were not committed, they are probably lost. If you have committed those changes, then you can do something about it.

Run git reflog to see the changes that happened. You might have to scroll a bit to see your previous commit, depending on what happened during the merge. Use git reflog --relative-date if required.

Just make a copy of the repo before proceeding to the next step just in case.

You’ll get something like this from reflog:

a190818 (HEAD -> master, origin/master, origin/HEAD) [email protected]{0}: reset: moving to HEAD
a190818 (HEAD -> master, origin/master, origin/HEAD) [email protected]{1}: pull origin master: Fast-forward
3cd9c9f [email protected]{2}: reset: moving to HEAD
3cd9c9f [email protected]{3}: pull: Fast-forward
e7467f1 [email protected]{4}: pull --tags origin master: Fast-forward
821119f [email protected]{5}: checkout: moving from 200-banks to master
821119f [email protected]{6}: checkout: moving from master to 200-banks
821119f [email protected]{7}: pull --tags origin master: Fast-forward
8913f7e [email protected]{8}: checkout: moving from blog-link-color-fix to master

Figure out which one of the above lines denotes your correct repo state. Let’s say the third line, ie 3cd9c9f. You then do git reset --hard 3cd9c9f. And the repo should be reset to that point.

Answered By – nullptr

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published