I'm trying to learn git, I'm new. I got an error while performing a pull from xcode and it was said on the internet that the answer was git reset --hard. I also performed git init and "git reset --hard" from the terminal. As soon as I performed this operation, almost all my files on my desktop were deleted, including my word, excel files and videos in my documents. I want to know what could be the reason for this. I would be grateful if you could help.
CodePudding user response:
This is probably a duplicate of How to remove all local files that were accidentally added to the git system.
Basically, it seems you said git init
when your terminal's current directory was your Desktop. Therefore, you accidentally placed your entire Desktop under Git control — and thus, if you proceed to give "dangerous" Git commands, such as git reset
or git clean
, you risk serious damage to your Desktop.
You did give a dangerous Git command; git reset --hard
is an extremely dangerous command. It means, "Force my working tree to consist of exactly what the most recent commit consists of." All files in your working tree that are not part of the most recent commit are thus deleted. But in your case, the Desktop was the working tree! Thus you wiped out your entire Desktop.
If you have not previously done an add-and-commit of all the files on the Desktop (and it sounds like you haven't), those files are now totally gone. There might be copies in a backup that you've made in some other way, but that would have nothing to do with Git, which has done exactly what you (unfortunately) asked it to do.
CodePudding user response:
The magic of Git is that it remembers everything you do, so most things you do can be undone, even deleted files and resets. We can try a few things and see what works, but since we can't know exactly what commands you did, there's no guarantee you'll be successful. Fingers crossed!
Before we start, make a copy of any files you've put in your desktop since the incident and put them somewhere safe, because they might get deleted in the course of our restoration.
Check Time Machine local snapshots
This one isn't specifically related to Git, but since you have a Mac, it's the simplest solution and worth a shot. Time Machine automatically captures and saves your files periodically (unless you turned it off), for exactly your kind of situation.
Open the Time Machine app (the app, not the one in System Preferences). Scroll through the timeline on the right and click the most recent snapshot from just before your Git mishap. You can click around in the Finder window in the middle to check if your files are there.
If you manage to find the right snapshot, click the Restore button, and your files are back! A safer alternative that makes sure you don't lose any later work elsewhere is to copy the files from your old desktop, exit Time Machine, and paste them into your current one.
Restore items backed up with Time Machine on Mac - Apple Support
About Time Machine local snapshots - Apple Support
git restore
If Time Machine doesn't have your files, Git might be able to restore them. I'll try to pick simpler commands since you're new to Git.
For the rest of these steps, you'll need to first make sure your .git
folder is still there. Head to your desktop, and press Cmd Shift . (period)
to show hidden folders, and look for one called .git
. Also check your Xcode project folder, your trash, and your user folder. If you can't find it, there's not much left we can do.
These steps will also only work if at some point you’ve committed the files on your desktop to Git (accidentally, presumably). Otherwise, if you reset without committing, they’re probably gone for good.
If it's there, we can probably restore your files. Open Terminal to the directory enclosing the .git
folder (in your case, probably ~/Desktop
). Run this:
git status
If you see Git showing a bunch of deleted files like this in red text:
Changes not staged for commit:
deleted: file1.txt
deleted: file2.txt
...then it's easy to simply use git restore <file>
to restore the deleted files (e.g. git restore file1.txt
). If there are a lot of files, you can run git restore .
(notice the period at the end) which selects all uncommitted files, but this will override any files you've subsequently added.
git reflog
and git reset
It's more likely that git status
told you something like "Nothing to commit, working tree clean," or the changes are all from more recent times. There's one last thing we can try. Run this:
git reflog
...and you should see a list of all the past actions you've performed with Git, including commits, pushes, pulls, and importantly, resets. If the list extends beyond your Terminal window, the final line at the bottom will be a colon :
, in which case you'll need to hit return
to scroll the list, and q
to exit.
Try and find an item in the reflog from just before your accidental reset. If you can't remember which it is, you can also scroll all the way to the bottom and find the very first item. Whichever option you choose, remember that everything in your Desktop will be reset to that point in time.
Once you've found the reflog entry you want, copy its ref. In this example, the ref is d3d1535
:
d3d1535 HEAD@{1}: commit: Some commit message
Now, run:
git reset --hard <ref>
Hopefully, you'll see all your files back. If not, you can try going back even earlier in the reflog history; just use the above command with whichever ref you want.
And if it still doesn't work?
Oh dear. Hopefully you'll have made some backups, because I can't think of much else that can be done. If that's the case, there might not be much else that’s possible.
So what did we just do?
As I said, the beauty of Git is that it logs almost everything you do into the reflog (reference log). Because you seem to have done most of your commands in Git, most can be reset. This is as opposed to other commands you might do in Terminal, which are more permanent.
If git restore
worked for you, that means you somehow deleted your files, but the changes hadn't been committed yet, and they were still in your working tree, in which case they could be restored very easily.
It's more likely you had to go through the reflog route. For this, we checked through Git's record of everything you did, found a suitable point to jump back to, and told Git to reset it to that point. The beauty is, even that reset isn't permanent; it just becomes another entry in the reflog which you can also revert. This is why Git is an essential tool: you can make big mistakes and undo them.
From now on
If all your files are back the way you want them, delete the
~/Desktop/.git
folder. Git is meant to track directories for individual projects, not your entire Desktop or Documents folders.When using Xcode or VS Code or any other editor, open it at a suitable directory, because they will initialize Git at the top level of the folder you have open. Don't open code editors to places like Desktop or Documents; keep editors and Git to lower directories where any damage will be contained. If you're doing
git init
in Terminal, make sure the path is the one you really want, and not the root directory.Be careful when typing commands into Terminal, especially ones that involve
sudo
(admin privileges),-f
(meaning "force"), and-r
(meaning "recursive"). Terminal isn't meant to be used by everyday users and so any mistakes can be very damaging.If you haven't enabled automatic Time Machine local snapshots, please do. They don't take up any space on your Mac and they've saved me more times than I can count.