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}

Ishank Gulati
  • 633
  • 1
  • 8
  • 22
Janet A
  • 11
  • 4
  • 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 '19 at 09:04
  • I did git add . – Janet A Jul 18 '19 at 09: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 to see the contents of each file.). Reference : https://stackoverflow.com/questions/5788037/recover-from-git-reset-hard – user Jul 18 '19 at 09: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 '19 at 09: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 '19 at 09:26
  • Looks like someone else pushed to the branch you're working on before you could, that's why git asked you to pull the latest changes. As @RajniKewlani said, regularly commit changes to prevent this. You can give intellij local history a shot if you're using the same https://blog.jetbrains.com/idea/2008/01/using-local-history-to-restore-deleted-files/ – Ishank Gulati Jul 18 '19 at 09:31

1 Answers1

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>
user
  • 867
  • 9
  • 21
  • 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. – PhiloEpisteme Jul 18 '19 at 14:04