Search code examples
gitdelete-filegit-checkoutgit-reset

Accidently removed files from local folder after running "git checkout origin/main"


I am new to git.

I accidentally deleted all project files from my local machines after running git checkout origin/main command.

I have checked and could not find the files in the Recycle bin as well. How can I recover those lost files?

I was following a script that I have written when I was learning Git and this was the first time that I tried it on an actual project. Following is the message I receive. Should I run git switch. If yes, how.

Message received after running the checkout command

I do not want to do something stupid again and loose whatever I am left with. How can I recover those lost files?


Solution

  • The good new is : the command your ran isn't a destructive command, you simply have switched from your initial commit to the commit currently at origin/main.


    The shortest way to get back to where you were before is to run :

    git switch -
    

    (in that command: - means "the state I was in previously")

    A more detailed way is to run git reflog, inspect the history of actions you did, and return to either a specific sha1 or to a branch because the associated message mentions : checkout: moving from <branch> to origin/main.

    (actually: git switch - is a shortcut to "inspect the reflog and go back to the last time it mentions 'moving from this to that'")