Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reconstituting Git History After Manual Copy of Repo Files

I have a been using a copy of a repository that was manually copied and, unfortunately, has no history. Now an older copy of the repo has been found with a large amount of history. What is my best bet for taking my repo (red) that was created from scratch and incorporating the history of the historical repo (blue).

git

like image 602
Steve Avatar asked Aug 01 '19 13:08

Steve


2 Answers

In your new repository, add the old repository as a remote and fetch it.

git remote add old <path or url to the old repo>
git fetch old

A - B - C - D [old/master]

              E - F - G - H [master]

E is your initial commit where you copied in all the files.

Check that D and E are similar with git diff old/master E. If they are the same, or near enough that it doesn't matter, rebase master on top of old/master.

git rebase old/master

            [old/master]
A - B - C - D - E1 - F1 - G1 - H1 [master]

                E - F - G - H

E1 will now be the diff between the files in D and E. If the files are identical there will be no E1 and it will skip to F1.

You can then git push this back to old.

git push old master

                                  [old/master]
A - B - C - D - E1 - F1 - G1 - H1 [master]

                E - F - G - H

Note that this is just for master. If there are other open branches they will also have to be rebased onto the new master. Also be sure to check if any tags need to be moved.

Now with history merged, decide which repository is canonical and destroy the other one to avoid history diverging again.


If there is an overlap, that is if changes were made in the old repo after file were copied to E, you may have conflicts. If a lot of changes were made in the old repo after the files were copied it may be necessary to rebase off of an older commit and then treat the newer commits as a branch.

For example, let's say the files were copied to E just after B was committed to the old repo. Then later C and D were added.

A - B - C - D [old/master]

      E - F - G - H [master]

You'd rebase on top of B.

      C - D [old/master]
     /
A - B - E1 - F1 - G1 - H1 [master]

        E - F - G - H

And then treat the remaining commits on old/master as any other old branch.

git merge old/master

      C - D ---------------   [old/master]
     /                     \
A - B - E1 - F1 - G1 - H1 - J [master]

        E - F - G - H
like image 185
Schwern Avatar answered Nov 16 '22 00:11

Schwern


I’m not sure if this will work since you have copied the repositories manually. This works if you have to repositories where you copied one of the repositories at one point and the commits have the same commit hash. Buts might be worth a try.

In your old repository:

Git remote add [name] <url to the new repo>

Now in your old repository (origin) you will have all the history to the point where you lost it and the new repository (the name you gave it in [name] in git remote add….) where you have all the new commits.

Now simply place yourself on latest commit on the master branch in the old repo and do a

Git merge [name]/master 

(or whatever your master branch is named)

The idea is to try to get something like this:

enter image description here

like image 44
Fisken Avatar answered Nov 16 '22 00:11

Fisken