2

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?

SJa
  • 487
  • 4
  • 14
  • Had you committed those files already? If they were committed, they should be in the Git history, you just have to find the right branch. – joanis Jun 22 '22 at 13:17
  • For example, if they exist in your local `main` branch, `git switch main` should bring them back. Use `git log --all` to look at the commit history and see if you find commits where they were added to the repo. `git log --all --stat` will show you the files changed in each commit. – joanis Jun 22 '22 at 13:19
  • And then the next thing I recommend is to invest a few hours to read through a Git tutorial and play with a toy repo where you won't care if you destroy stuff, until you get familiar with how Git works. That will be time well spent, you'll be happy later to have done it. I have a `just-testing.git` repo on various Git servers I use just for testing stuff out whenever I'm learning about a new feature or testing stuff to answer a question about Git on SO. – joanis Jun 22 '22 at 13:22
  • 1
    And back up your entire hard drive first because if you give a certain two Git commands you can erase your entire hard disk. – matt Jun 22 '22 at 13:24
  • To get back to your question, `git switch` switches to the commit or branch your specify, replacing all the files from the commit where you are currently at with the commits where you are switching to. So yeah, it's normal files would disappear, appear or get modified by a `git switch` operation. – joanis Jun 22 '22 at 13:26

3 Answers3

1

Yes, say git switch - and see if all the files return. I think they will.

I don't think the files were ever deleted. You need to understand that the project files you see are not the real project files. The real files are hidden in the real Git repository, which is invisible.

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

I don't think so. I think you just did a checkout of a different branch — to a branch where those files don't exist. You need to understand what branches are.

matt
  • 515,959
  • 87
  • 875
  • 1,141
1

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'")

LeGEC
  • 46,477
  • 5
  • 57
  • 104
1

git checkout can be dangerous because it operates both on files and branches.

That is why Git 2.23+ (Q3 2019) has introduced git switch (man page)

It would not switch if the working tree had files which would be removed or modified. If your have disappeared, it means they were committed.

- is a shorthand for @{-1} which is specified in gitrevisions:

The construct @{-<n>} means the <n>th branch/commit checked out before the current one.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250