1

One of my coworkers created a new git repository the "wrong" way, by copying the tree, deleting the .git directory, and doing a new git init. The coworker then dramatically slimmed down the repository, removing a bunch of files and renaming others (before committing anything).

It seems to me that it should be possible to restore the history by making a diff from the original repo to the new one, and then patching.

I was able to generate such a diff using

diff -ur old new --exclude=".git" > master.patch

I then almost got it to patch by doing

git clone old new_old
cd new_old
git checkout master
patch -p 1 < ../master.patch

# Then, if the patch command had worked, I would have done:
cd ..
rm -rf new
mv new_old new

(-p 1 tells it to chop off the repository directory name) but it seems to run into problems with files that were completely deleted.

I expect it'll also run into problems with files that have been renamed (e.g., from old_dog.hpp to new_dog.hpp). But I've also read that git

I'm sure someone else has done this before, but I can't find any examples. Is there a simple way to restore the history for files in this repository, or am I just wasting time?

Translunar
  • 3,739
  • 33
  • 55

1 Answers1

1

It's a lot simpler, actually.

With an old .git directory, remove all the files and replace them with all the files from the new root commit.

Then, if there are any other commits on top of the new root commit, use git format-patch to generate a series of patches and git am to apply them. (You could instead perform a rebase but that is needlessly complicated for this case IMO)

o11c
  • 15,265
  • 4
  • 50
  • 75