0

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}

6
  • git reset --hard is a potentially dangerous command, since it throws away all your uncommitted changes. For safety, you should always check that the output of git status is clean (that is, empty) before using it. Did you add/commit your changes ever ? If you did not add/commit your changes before using git reset --hard, unfortunately there is no way to get them back because git did not track them ever.
    – user
    Jul 18, 2019 at 9:04
  • I did git add .
    – Janet A
    Jul 18, 2019 at 9:06
  • 1
    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 <filename> to see the contents of each file.). Reference : stackoverflow.com/questions/5788037/recover-from-git-reset-hard
    – user
    Jul 18, 2019 at 9:11
  • @RajniKewlani still cant find my files.. I think it permantly deleted files. F**ck real, so many hours gone and im pissed off.
    – Janet A
    Jul 18, 2019 at 9:20
  • If you are not able to find it in lost-found and you don't have it in IDE backup (try to find it in IDE backups eclipse/intellij etc keep some temporary backup copies) then as far as I know unfortunately it is lost :(
    – user
    Jul 18, 2019 at 9:26

1 Answer 1

1

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:

  1. 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/).

  2. 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/).

  3. 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>
1
  • 1
    I'll just add that because OP did git reset --hard HEAD@{12} that at least those last 12 commits can be recovered using the reflog. @Janet A, the reflog can be slightly annoying to read. What you'll want to look for is a line that reads something like reset: moving to HEAD@{12}. The line JUST below that will contain the commit hash of the one you're interested in. Jul 18, 2019 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.