5

I accidentally did something to my git repo and I don't know if I can save my project at this point...

I had a bunch of changes that I made. Then I wanted to delete my last commit so that I could make this new commit instead. I forgot to do git stash. So when I ran git reset --hard [second-to-last commit] it erased everything I had done. It was stupid, but is there anything I can do to rescue my recent work?

I'm using Eclipse IDE.

scharette
  • 9,437
  • 8
  • 33
  • 67
brienna
  • 1,415
  • 1
  • 18
  • 45

4 Answers4

3

Short answer for git, NO.

It is impossible to recover file that you did not add or stash


In general the best way to handle these kind of problem is to rely on the IDE instead.

In Eclipse, you could look under this path

.metadata/.plugins/org.eclipse.core.resources/.history/

So, you could use those commands to find the most recent changes,

cd .metadata/.plugins/org.eclipse.core.resources/.history/
ls -al * | grep "<today's date>" | grep "r\-\-" | sort -k 6

Note that you will have to replace "<today's date>" with "Jul 27" for example.

Then you could use

find . -name <filename>

Note that you will have to replace "<filename>" with something like "7098a672a2bc00111703c0e5cbee369d" found with the previous command.

For more info look at this.

scharette
  • 9,437
  • 8
  • 33
  • 67
2

Unfortunately, git has no way of knowing about the file contents that you didn't commit or stash. This means you cannot restore your changes with git. Other tools might be able to help. For example, IntelliJ has a "Local History" feature that tracks all saved changes. Perhaps your editor has something similar?

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • I used Eclipse. – brienna Jul 27 '18 at 17:43
  • @briennakh [This may help](https://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.platform.doc.user%2FgettingStarted%2Fqs-55.htm). – Code-Apprentice Jul 27 '18 at 17:44
  • Git has nothing to with it. You not only ran a `reset`, but you also passed a `--hard` flag. Instead of staying away from it, learn the basics. Git is truly powerful when correctly understood. – scharette Jul 27 '18 at 17:52
  • 1
    @briennakh Git is a great tool. You should take this as a learning experience to be more cautious with any git command rather than throwing it out entirely. – Code-Apprentice Jul 27 '18 at 17:54
  • Yeah, just frustrated @Code-Apprentice – brienna Jul 27 '18 at 18:04
  • @briennakh I feel your pain. Look at the link I have to eclipse help files. You should be able to figure out something to help restore your work using that tool. – Code-Apprentice Jul 27 '18 at 19:23
2

You mentioned in your comments that you were using Eclipse - normally eclipse keeps a local history of changes that you were and have been making. The following steps may help in that regard:

Restoring deleted resources from local history To restore a deleted Workbench resource with a state from the local history:

In one of the navigation views, select the folder or project into which you want to restore a local history state.

From the resource's pop-up menu, select Restore from Local History.... The Restore From Local History dialog opens showing all files that were previously contained in the selected folder or project and all of their sub-folders.

Check the files that you want to restore

If you don't want to restore just the last state of a file you can select any other state of the file from the Local History list on the right hand side of the dialog. The bottom pane of the dialog shows the contents of the state.

If you are done with all files click Restore.

Tip: You can configure your Workbench preferences to specify how many days to keep files, or how many entries per file you want to keep, or the maximum file size for files to be kept with the command link General > Workspace > Local History preference page.

this can also be found at this link: https://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.platform.doc.user%2Ftasks%2Ftasks-87b.htm

Bob
  • 388
  • 5
  • 19
0

Using the following command worked for me,

git reset --hard ORIG_HEAD

Please check this answer for more details.

  • This is useful to know, but it'll recover the last commit. not the uncommited changes that the OP wanted recovered. – NickSoft Aug 08 '23 at 15:18
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 09 '23 at 08:28