Reputation: 13
I messed up my project and spent hours trying to fix it. I gave up, deleted it and then had to download the one I have working on my GitHub. I unzipped the file got it running on unity again done a good bit of work. Then I wanted to commit it to GitHub and I was getting a Git error when I tried to push so I took this guys advice who told me to:
.git/
directory from the folder. Then execute these commands:git init
git add .
git commit -m "First Commit"
git remote add origin [url]
git push -u origin master
When I deleted my .git/
and pushed the folder to my repo it got rid of my commit history. I've tried looking around for solutions like git reset --hard b1719639
but I can't revert back to my previous commits as they're gone since I deleted my .git/
from the folder when I committed. I still have the .git/
folder.
How can I fix this?
Upvotes: 0
Views: 915
Reputation: 137227
Generally speaking, don't touch your .git/
directory. Use git
commands (ideally high-level "porcelain" commands more than low-level "plumbing" commands) and let it modify what's in .git/
.
Even more importantly, don't run commands you don't understand. Blindly copying and pasting commands is a good way to get into trouble. Yes, ask questions. Yes, search the Internet. But before you use what you find, read about it. You want to know what it's supposed to do before you try it. This is especially important for any command involving sudo
, but it's good advice in general. I'm going to try to clearly explain each command I suggest running below. If you don't understand one of them, please look at the relevant documentation or ask for clarification.
Okay, now, let's see if we can get out of this mess. You say you have a backup copy of the old .git/
directory. This should contain all of your old history. Back the new .git/
up and put the old .git/
back, then run git log
to see your local history. Hopefully you'll see your old commits.
Then run git status
. I think you'll find a bunch of changed files. Run git diff
to see what the differences are between your working copy and your last commit. It should be all of the changes you made since you first deleted the .git/
directory.
If these changes make up one logical commit, great. You can add them and commit as usual. If they should be multiple commits you can make multiple commits. If you have several commits in the second .git/
directory that you want to keep you may be able to add that as a file://
remote, fetch a branch, and do some merging / rebasing / cherry picking. This part is obviously very dependent on what you have and what you need.
Finally, once you see what you expect in git log
, you're going to have to push back to GitHub. Once again, you'll need to do a force push. Generally speaking, force pushing is a dangerous operation (as you've learned), so be careful when using it¹. In this case it probably makes sense.
¹In most cases --force-with-lease
is preferable to --force
. In this case I don't think fetching the old refs from GitHub to make --force-with-lease
work is worth it.
Upvotes: 1
Reputation: 1177
Most likely you won't be able to recover the history. However when you ran git init
your local repo was reinitialised to the HEAD of the github repo. At this point, if you run git status
you will still see the changes between the github HEAD and your current status. Make sure you commit and push those. Do not checkout or reset, otherwise you may lose everything for good.
Upvotes: 0