Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to undo a git restore to get back changes

Tags:

git

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

like image 420
Michael Albert Avatar asked Mar 05 '20 22:03

Michael Albert


Video Answer


3 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.

like image 83
Ankit Singla Avatar answered Oct 24 '22 00:10

Ankit Singla


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.

like image 23
parse Avatar answered Oct 24 '22 00:10

parse


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

like image 6
Islam Murtazaev Avatar answered Oct 23 '22 23:10

Islam Murtazaev