Reputation: 1207
My current situation is this one:
Question:
How could I recover all lost history from repo1 and add it to repo2?
I basically would need all my code history in repo2.
Upvotes: 2
Views: 45
Reputation: 45819
Getting all of the content into repo2 is the easy part. The bigger question is how important it is to you (or other users of repo2) for the resulting history to be seamless.
From repo2, you can add repo1 as a remote and fetch the history.
$ cd path/to/repo2
$ git remote add old-history path/to/repo1
$ git fetch old-history
In a simplified world where each repo just had a master
branch, this would give you something like
O -- x -- x -- x <--(old-history/master)
O2 -- x -- x <--(master)
so the history is there and can be queried, but you'd have to go out of your way to see it. (If each repo has multiple branches, the picture is more complex, but basically the same.)
Assuming that old-history/master
has a commit with the same content as O2
you could use git replace
in an individual clone to make the history appear more or less seamless. (You could always add such a commit to old-history/master
if there isn't one.) There are some quirks with git replace
, so if you want to go that route, refer to the docs at https://git-scm.com/docs/git-replace
You also could do a history rewrite to permanently combine the history. This is a more extreme measure and requires some coordination with any other users who share repo2
, but it is a "do once and it's done" solution (in contrast to replace
, which only affects one local clone).
To do a history rewrite, you could use the --parent-filter
of git filter-branch
(see https://git-scm.com/docs/git-filter-branch), or presumably you could use git filter-repo
(though I don't know much about that command). There are caveats to doing a rewrite of any kind. You can search SO for numerous past discussions of this procedure, or refer to the git docs for information about rewriting shared histories.
Upvotes: 2