While pushing new commits to upstream repo, I got an error that I need to pull new changes before pushing but this resulted in losing some of my changes.
The command I executed is:
git reset --hard HEAD@{12}
While pushing new commits to upstream repo, I got an error that I need to pull new changes before pushing but this resulted in losing some of my changes.
The command I executed is:
git reset --hard HEAD@{12}
Listing possible states before git reset --hard was executed (in this case git reset --hard HEAD@{12}).
Apart from 12 commits (which are recoverable), about your other changes:
You just made changes and did NOT do git add
or git commit
.
Recovery : You cannot recover those changes because it does not exist in git records. Though, you can give a try to IDE local backup copies (for intellij : https://blog.jetbrains.com/idea/2008/01/using-local-history-to-restore-deleted-files/).
You just added it using git add
but did not commit it.
Recovery: No guarantee but you can try to recover.
Previously staged changes (git add) should be recoverable from index objects, so if you did, use git fsck --lost-found to locate the objects related to it. (This writes the objects to the .git/lost-found/ directory; from there you can use git show to see the contents of each file.) If you were lucky and your IDE took backup, you can check if IDE saved your life (intellij : https://blog.jetbrains.com/idea/2008/01/using-local-history-to-restore-deleted-files/).
You committed changes using git commit
Recovery: Yes, YOU CAN RECOVER in this case. Use:
git reflog
to get the commit-hash of your commit. Then use:
git reset --hard <commit-retrieved-using-reflog>