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 ?
-
1Even when used locally, it's good idea to have two copies, the "upstream" repository you push to and pull from, and a clone where you do actual work and commits. That way if things go haywire, just clone the "upstream" again and continue from.version you pushed last. You can't really damage the upstream copy with push so even if you push bad stuff, old versions are still there. – hyde Feb 28 '13 at 05:42
-
how do you do that .. just tell me what to google for ... :) – Samhan Salahuddin Feb 28 '13 at 05:45
1 Answers
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
-
but how on earth could that recursive folder thing happen ... it seems quite hard to do even intentionally .... – Samhan Salahuddin Feb 28 '13 at 06:58
-
What really happened is hard to know without seeing all the commands you actually did... – hyde Feb 28 '13 at 06:59
-
I added a piece about creating a local backup repository, and a link to managing new branches under workdir. – hyde Feb 28 '13 at 07:10