0

Note this is NOT a complete duplicate of Detach (move) subdirectory into separate Git repository, which shows how to correctly carve out a subdirectory from an existing repo and create a new repo for it so history is ported to the new repo. But it doesn't show what to do if the subdirectory was incorrectly carved out, hence not porting the history, and how to port it later.

In my case, someone in my company moved an existing directory in an existing Git repo months ago into its own Git repo. That is...

Old repo structure

/projectDir/module1
           /module2
           /module3

to

Repo1:
/projectDir/module1
           /module2

Repo2:
/module3

However, the person that did it did NOT follow the above post when the subdirectory was carved out into a new repo, and hence the history wasn't ported over from the old repo to the new repo. In the meantime, two things have happened, namely,

  • The subdirectory was removed from the old repo
  • the new repo has been used, thereby accumulating its own new history

How can I port the history over from the old repo to the new repo, while preserving the current history of the new repo.

Chris F
  • 14,337
  • 30
  • 94
  • 192
  • 2
    The post https://stackoverflow.com/questions/359424/detach-move-subdirectory-into-separate-git-repository shows how to do it correctly, that is, while the new repo is being created. In my case it's been months since the new repo has been carved out from the old repo. Is the "duplicate" post still pertinent? – Chris F May 11 '21 at 17:04
  • I'm sure it is. First you detach a subdirectory using `git subtree split`, then you import the new repository using `git subtree pull` – phd May 12 '21 at 23:51

1 Answers1

0

Here's what I had to do

  • cd to /path/to/old/repo
  • Run git log on the old repo to find out when the module3 subdir was removed from the old repo, and take a note of the commit hash
  • git checkout <commit hash> the old repo
  • Run git subtree split -P module3 -b module3-only
  • cd to /path/to/new/repo
  • Create branch git checkout -b port-old-history
  • Run git pull /path/to/old/repo module3-only --allow-unrelated-histories
  • git push origin port-old-history
  • Create PR to merge port-old-history to master
Chris F
  • 14,337
  • 30
  • 94
  • 192