For the past 2 weeks I’ve been working on a new website that contains quite a few custom HTML elements. After finishing a huge chunk of the work, I decided to push the project to GitHub, as I usually do for all big projects.
As I went to push the project up to GitHub, I noticed that there were some changes to the repository that had to be pulled first. As tired as I was, I went ahead and pulled these changes without actually realising that I was syncing my work with the incorrect repository! Then I headed off to bed for some well needed sleep.
The next day I came on and freaked out to see that ALL of my work had gone – everything. I was like “wtf man, has someone been on my laptop whilst I was sleeping and deleted everything?”. Then I began to investigate.
I opened my project in Finder and noticed that all PHP files were in fact gone from the directory. However, I also noticed that the project folder structure was still intact.
I then opened Sublime Text and noticed that all PHP files were inside the temporary file buffer, all code within them had gone, but they could be saved as empty files. I tried undoing changes, but unfortunately there weren’t any to undo. Damn.
I then decided to delete the .git folder and re-initialise the repository to see what was going on* (big mistake – see later comments).
I then browsed the repository on GitHub and noticed that something strange had happened – the repository had merged code with a previous project I was working on. I soon figured that I must have added the wrong repository URL when setting up the repository on my local machine. Damn.
After spending about an hour Googling for a solution, I came to accept that I had lost my work and that I would have to face my mistake and start my project again.
Just as I was about to give up hope, it hit me that Chrome stores cached versions of anything you access within the browser, so I decided to open the cache and see what I could find. I did this by typing the following into the Omnibox in Chrome:
To my surprise, I found cached versions of every single file/page I had created for the project! I saved those cache files and used this extremely helpful tool to decode the raw binary versions of the cache files back into human readable text.
Now that I had the HTML and CSS back for each file within my project, I began to copy and paste each file back manually until I had a usable project again which navigated to multiple pages. I still had to code all of the PHP within project again, but this was nothing more than setting up a few URL routes, which only took a few hours. A few hours is still a long time, but it certainly beats rewriting everything from scratch!
So, how could this issue been completely prevented? Well, here are a few tips I would suggest doing whilst working on a project:
- Backup. We’ve heard it a million times before, but we still don’t do it. You can do this by zipping your project every now and then, then archiving it safely on an external storage device or cloud service.
- Turn on the autosave feature in your text editor. My text editor of choice is Sublime text which supports automatic backups of your projects.
- Leave Google Chrome to cache files so you can recover data. This is what really saved my ass in this particular instance.
- Don’t re-initialise a git repository if something does mess up. You can always rollback to a previous commit to recover your work. This is something that I completely forgot about before deleting the .git folder!
- Always confirm that you’ve added the correct remote URL of a repository before pushing up your code. You can do this by typing the following command into your terminal:
git remote -v
This will list both, your fetch and push repository locations so you can confirm that they’re correct.
origin https://github.com/jjmpsp/xxx.git (fetch) origin https://github.com/jjmpsp/xxx.git (push)
I hope you found the tips in this post useful and you can recover your web project if it has been lost, for whatever the reason may be.