How to undo a git restore to get back changes

Viewed 35421

Here is my situation, I'm still new to git and while I was working on a project I didn't constantly commit.

I finished the project and added everything other than the index.html file and committed them. I pushed the changes to my repository. I then checked my git status and saw that index.html was still listed which I thought was a mistake and maybe I just did a little edit by accident or something so I did a git restore index.html.

I almost immediately realized what I did and checked the file to see that the file had been reverted to its original form. Please tell me there is a way to get my changes back. I just want to emphasize that the command I used was git restore index.html

8 Answers

If the file you accidentally restored is currently open in your Text Editor or IDE, a simply Undo operation would do the trick and get you your changes back.

I accidentally typed git restore . while I only wanted to restore one file. Luckily for me, all the files that had been restored by the command were still open in VSCode, where I made the changes, so executing an Undo in each of those files did the trick for me and I got my changes back.

I strongly recommend to use any local history plugin for your IDE if it doesn't have a built-in one. Even that version controlling tools are meant for this purpose, but some developers who just met and recently started using git get this wrong! Well, sometimes you will lose ton of work because of making a noob git mistake that is impossible to UNDONE.

In my case, I use Local History for VSCode. It's like a backup I have all time if something goes wrong. Just use it, it will cost you some disk space but you can clean it up whenever you are 100% sure that the current code you have now.

If you are using JetBrains IDE, you can get back your changes in multiple files.

You need to go to each file and revert to the last change on local history (not stored in git).

If you never added your index.html changes to your git index then git won't have any record of them. The only way to recover them would be with whatever reversion tools your operating system or backups provide.

If you know where the last changes were made and you didn't close the IDE, just go to that particular file and try undo (ctrl+z).

For Web (CSS and Javascript), you might be able to get the source out of the browser inspector if you haven't reloaded the page and it's not minified. This just now saved my grits.

For Eclipse with Git integration

  1. Open a context menu (by right click or menu key) on a file in the Project Explorer or the editor area.

context menu open

  1. Team, then Show Local History

local history window

This will list all recent edits by date and time. This works even if you've lost the changes in the undo/redo list.

  1. Activating one of the entries will open a new editor window showing what the file looked like at that time. Notice that this window is in read-only mode, so you can select and copy its contents.
Related