Samhan Salahuddin
Samhan Salahuddin

Reputation: 2170

Git repository corrupted

I was trying to learn how to use Git for local version control . I was using Xcode as my IDE . I tried to add my latest changes when I found that there were 3000 unstaged changes . I staged them unknowingly. I then went to my project root directory to find the same directory inside it with the same files and so on inside all the way down . I deleted it and the repository got into a corrupt state as the staged files were missing . What was i actually supposed to do and how could this have happened ?

Upvotes: 0

Views: 184

Answers (1)

hyde
hyde

Reputation: 62777

If you deleted the .git folder, then you deleted the actual local repository clone. Salvage your sources (hopefully you have a good version somewhere) and init a new repository with them from scratch.


If you want to create a backup repository, when you already have one where you do work, this is perhaps the simplest way, after making sure you have no uncommitted changes you want to keep (bash commands, but you get the idea even if you don't use bash, I'm sure):

cd work/myproject
cd ../..
mkdir backup-upstream
cd backup-upstream
git clone --no-checkout ../work/myproject

Then you'll never need to touch that backup directly again. Now you could set your current work/project to follow that, but it's simplest and least error-prone to just do a fresh clone, continuing from above:

cd ../work
mv myproject myproject-backup
git clone ../backup-upstream/myproject
cd myproject

Now it can be enlightening to check out the difference of these files, to see what actually changed:

cat .git/config
cat ../myproject-backup/.git/config

Then you work under work/myproject as usual, and when you want to create backup, just git push. The clone under backup-upstream should be thought of as any remote upstream repo, for example from github, it doesn't matter that it's actually accessed from local filesystem. Note, that if you create new branches, you need to push them separately, see: How to copy a local Git branch to a remote repo

Upvotes: 3

Related Questions