Home > Back-end >  Accidentally deleted .git/objects folder
Accidentally deleted .git/objects folder

Time:09-28

I Accidentally deleted .git/objects folder. I have had commits that haven't been pushed to the remote yet so i'm worried that some changes are lost. What can i do now to restore them?

CodePudding user response:

If it has been deleted, like for real..... not much git can do here. You will need to do some work to be able to resume work, but forget about the revisions you had created locally. So... clone a second repo from the original repo that you started working from (assuming you are working as people normally do these days using a central repo to work against).

When you have that, create a branch (what will be your branch from now on) on the latest revision that you were able to merge from the upstream branch in the original repo (assuming it's not the tip of the upstream branch.... but it could be the tip).... or, if you never merged, then set the branch at the same commit where your branch was started from.

Checkout that branch and now replace the working tree with what you have in the working tree of the broken repo (do not bring anything from git's database).... now you can resume working on that branch.

By the way, I am making a bunch of assumptions:

  • You had a single branch laying around with changes that had not been pushed into the remote. If you had more branched that had not been pushed, you are out of luck because you won't be able to get the back from the broken repo.
  • You were working against a single branch while you were developing. If you had been, say, cherry-picking stuff from a different branch than your upstream, then it needs more work to be able to be in a resumable state.

And there could be others that my subconscious me is hiding from me to make my life simpler.

CodePudding user response:

If you cannot restore the folder like C1sc0 suggested, a fairly simple solution would be to just clone the git project into a new directory, then compare the folder where you deleted the .git folder with the folder you just cloned the git project inside. Some IDEs have a very good compare function that makes this quite easy. Then just paste the changes over, commit and push. After you are sure everything works, you can delete the folder where you deleted the .git folder from, as the new one should have all your changes.

CodePudding user response:

If you are on windows you can restore it from the recycle bin. (And similarly on linux from Trash) (except if you deleted with shift delete)

  •  Tags:  
  • git
  • Related