1

I updated the branch and meanwhile I pulled the latest from that branch. After I pulled the latest and I saw couple of things were broken while building the branch and rather than fixing those I immediately ran

git reset --hard, at that moment I didn't even realize I had made changes on couple of files. Is there a way to recover my changes back?

Dip
  • 57
  • 5
  • 1
    I'm nearly certain the answer is no. If you didn't commit or stash, your changes are gone. But maybe a wiser Git guru knows better. – Marc Jan 30 '20 at 03:12
  • wait, if you are in the middle of a bad merge, you can simply write `git merge --abort` and all will be fine – BenKoshy Jan 30 '20 at 03:20
  • It would be really helpful to have that tip. However I am also doubting there's a way to get that back though. – Dip Jan 30 '20 at 03:22
  • Makes perfect sense. But I almost need to start from scratch now. Thanks for help. – Dip Jan 30 '20 at 03:28
  • If your filesystem does automatic snapshots, you might be in luck with a roll back, but as others have stated (and assuming you're not fumbling with the lower level plumbing commands), if the work wasn't committed, it's gone -- been there, done that. – Rafael Jan 30 '20 at 03:46
  • @Liam, They are mostly talking about staging or stashing or at least use git add . to index file to git. But in my case it's different I didn't commit, stash and the file was already in git, I just updated that one. Even though I tried `git fsck --lost-found` but no luck. Seems like my changes are lost. There's no way to get that back. – Dip Jan 30 '20 at 14:27
  • Yes, you can't. This is still a duplicate. The answer here covers it when it says [You cannot get back uncommitted changes in general.](https://stackoverflow.com/a/5788069/542251) – Liam Jan 30 '20 at 15:41

4 Answers4

1

How would git be able to recover something you have not committed?

If you had already committed those changes, and then had done something like git reset --hard then that's a different matter - you can recover those commit(s) from the reflog. But as far as I know, git can't recover something that was never committed.

BenKoshy
  • 33,477
  • 14
  • 111
  • 80
  • That's the main matter, I didn't stash or commit my changes. I did reset without thinking about what I was doing. I am damn foolish. – Dip Jan 30 '20 at 03:25
  • Its a lesson learn. I will think twice, thrice or 10 times before running `reset` or `abort`. – Dip Jan 30 '20 at 03:30
1

If you had not commited, staged, or stashed the changes you made, there is no way you can recover those changes.

Recovering lost changes. There are 2 SO question for this:

If you have ever committed some change and have lost that commit (like committing in a detached state), you can find that commit using reflog. See this SO question*.

If you have lost your last staged changes, you can also recover that. See this SO question*. (I have never used or tried it myself).

If you have stashed a change, you can also recover that using pop or apply. (I am not sure if the popped/dropped stashes are also recoverable that were not committed). You may find this Recover dropped stash in git useful.

Zig Razor
  • 3,381
  • 2
  • 15
  • 35
0

To prevent this kind of situation, instead of reset --hard I suggest you to git stash. Here you can find the documentation. This command allow you to keep clean working directory but pushing in a separate place your changes. You can have more stashes. You can name stashes. This answer does not help you now. Hope this answer will help you in the future.

sensorario
  • 20,262
  • 30
  • 97
  • 159
0

I'm afraid to say, there is no way you can recover your code if you run git reset --hard before commit or stash your code. But if you use modern IDE like IntelliJ IDEA, Pycharm or Goland then you can check local history and might get back some code.

Shubho Shaha
  • 1,869
  • 1
  • 16
  • 22