Home > Net >  Deleted entire Git project
Deleted entire Git project

Time:10-27

I tried to fix my git repo and .gitignore for not having tracked the right files and everything is gone from remote and from local. This is some history of commands in bash on my ubuntu server:

 1952  git status
 1955  git rm -r --cached .

 1957  git commit -m "git fix"
 1958  git push

 1981  git rm -rf data/
 1982  git rm -r --cached data
 1983  git clean -xdf

 1986  git ls-files
 1995  ls

The major issue is everything is gone locally or remotely. Thanks and Regards,

CodePudding user response:

Some Git commands are "dangerous": they wipe out actual files from the world of your visible files (the working tree). And they do not just conceal them in some tricky way; they just remove them, as if they had never been. You have chosen to use two of those commands: git clean and git rm.

So if you use those commands, you can certainly remove files in such a way that they can never ever be recovered.

However, the job of Git is to preserve earlier states of your working tree. So if you made any commits that included any state of any of these files, then those states are preserved in those commits. So all you have to do is one or both of these:

  • return to an earlier commit that includes your files

  • extract an individual file from an earlier commit that includes it

Since everything was pretty much okay in the commit before the one you just made, you can go back to that commit simply by saying

git checkout @~

If you're pretty happy with what you see, then you will also want to get out of detached head mode by resetting your branch to what you see now:

git reset HEAD

But if there are other files that were in your working tree and have never been committed, then that is no business of Git's and no fault of Git's: you are the one who used the very dangerous words clean and rm, so whatever you damage you did, you did it to yourself. Git only stores commits; that is all it knows about and all it cares about.

So what's the lesson here? Is it "don't ever give dangerous commands"? Not at all! Give any commands you like, but first you need to be safe. To preserve a file (meaning a file in a particular state), commit it — and do that before you start rattling around giving dangerous commands. To risk losing a file when you give dangerous commands, don't commit it. It's as simple as that.

  • Related