Search code examples
gitgithubversion-controlbitbucket

local changes are lost when using git


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) HEAD@{0}: reset: moving to HEAD
    a190818 (HEAD -> master, origin/master, origin/HEAD) HEAD@{1}: pull origin master: Fast-forward
    3cd9c9f HEAD@{2}: reset: moving to HEAD
    3cd9c9f HEAD@{3}: pull: Fast-forward
    e7467f1 HEAD@{4}: pull --tags origin master: Fast-forward
    821119f HEAD@{5}: checkout: moving from 200-banks to master
    821119f HEAD@{6}: checkout: moving from master to 200-banks
    821119f HEAD@{7}: pull --tags origin master: Fast-forward
    8913f7e HEAD@{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.