This week as I was working on a project, I made a terrible mistake running some random Git commands that I copied from the internet and ended up deleted the whole project.
How did we end up here?
Ok, the story is, I was trying to back up my project to my remote repository using git commands. I initialized my remote repository with a .gitignore
file and I don't usually do this. When trying to push the project after committing my changes I got the following error:
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
What deleted my project?
Just like any other software developers when your house is on fire you think of one place, Google. I typed some stuff and ended up on Stack Overflow. I was so happy when I found a question relating to my struggle (you know the feeling right?). The worst thing I did was picking the wrong answer. I copied and pasted the following command on my Git cmd:
$ git fetch --all && git reset --hard origin/master && git pull
After hitting enter my project was gone.
How I recovered my project?
I followed the following steps:
- Checking when the tip of my branches was updated
$ git reflog
This gave me the following information:
94e51c5 (HEAD -> master, origin/master) HEAD@{0}: reset: moving to HEAD@{1}
94e51c5 (HEAD -> master, origin/master) HEAD@{1}: reset: moving to HEAD@{2}
94e51c5 (HEAD -> master, origin/master) HEAD@{2}: reset: moving to HEAD@{1}
94e51c5 (HEAD -> master, origin/master) HEAD@{3}: reset: moving to HEAD@{0}
94e51c5 (HEAD -> master, origin/master) HEAD@{4}: reset: moving to 94e51c5
94e51c5 (HEAD -> master, origin/master) HEAD@{5}: reset: moving to HEAD
94e51c5 (HEAD -> master, origin/master) HEAD@{6}: reset: moving to origin/master
52d867f HEAD@{7}: commit: added a new link
80e3e2a HEAD@{8}: commit (initial): initial commit
- I wanted to go back to the first and what I did was:
git reset HEAD@{8}
HEAD@{8}
was for my initial commit (last line on the results above)
This listed all the files that I had added and committed with a D
on my CMD, showing that they were deleted.
- To recover them I ran the command below:
$ git restore .
My project was back
Top comments (0)