Execute the following commands from the parent directory above your repo:
Note: Replace 'foo' with the name of your project folder
1. Create a backup of the corrupt directory:
cp -R foo foo-backup
2. Now make a new clone of the remote repository to a new directory:
git clone [email protected]:foo foo-newclone
3. Delete the corrupt .git subdirectory:
rm -rf foo/.git
4. Move the newly cloned .git subdirectory into foo:
mv foo-newclone/.git foo
5. Delete the rest of the temporary new clone:
rm -rf foo-newclone
On Windows you will need to use:
- copy instead of cp -R
- rmdir /S instead of rm -rf
- move instead of mv
Now foo has its original .git subdirectory back, but all the local changes are still there. git status, commit, pull, push, etc. work again as they should.