mcint
mcint

Reputation: 944

Overwrote .git directory, recovering state from objects

I careless copied .git from one project (A) to another (B), with a remote scp -r (A) (B).

I would like to recover a usable state of the git repo. I can remove (A)'s .git objects. From there, how can I best go about finding tips of git branches and rebuilding a useful state of the repo.

Unfortunately, I don't have another copy of (B), and my mistake wiped out the metadata.

Upvotes: 1

Views: 55

Answers (2)

jthill
jthill

Reputation: 60403

Your only problem is restoring the refs. All B's objects are still there, but any of A's names (branches, remotes, tags, ...) that matched B names overwrote the object pointers.

Start with

git fsck --root

which will show you dangling commits and all the roots, most projects have just one, you can then do git log --graph --decorate --oneline --ancestry-path $the $dangling $commits --not $the $roots. Since the copy also overwrote your reflogs, if you stomped on all the refs this will find all your missing history.

Upvotes: 1

code_fodder
code_fodder

Reputation: 16371

For A you should be able to just clone a fresh copy of it (if there is a remote) into a new folder "A_copy":

  • git clone <url> A_copy

or for a specific branch:

  • git clone <url> -b some_branch A_copy

Then take the .git folder from this copy and drop it into your "broken" A. Then use git status to see what has changed.

If you want to revert either just use your new clone or you can do:

  • git reset HEAD --hard - this takes you back to the state of HEAD.

For B, if you at least have the files and you want to turn it back into a repo you can just do the following:

  • git init - makes the folder a git repo
  • git add -A - note this will add everything in this folder so tidy up first if you need to and maybe add a .gitingore if required.
  • git commit -m "gehhh... had to start again :("

Upvotes: 0

Related Questions