1

I'm creating a monorepo from some existing repos, and I want to preserve the commit history.

This answer here tells us that the git subtree command is good for this, and it works fine if we want to import the old git repos as is to the new structure.

However, the I have one project I'm important that looks like this:

/
  server/
    index.js
    package.json
  src/
  package.json 

Here, I want to import just the server folder, and retain the commit history on it.

I could just import the whole folder, delete everthing I don't need, and then shift everthing in server up one folder, but I'm worried about destroying the commit history.

Is there a way to import just a sub directory?

Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188
dwjohnston
  • 11,163
  • 32
  • 99
  • 194

1 Answers1

1

As Serge suggests in the comments - git filter-branch can be used to achieve this.

Here's the way I did it, perhaps there's an easier way to do it.

  1. Create a temporary remote repo 'temp'.

  2. In existing repo:

git filter-branch --subdirectory-filter server -- --all 
git remote add temp git@temporaryrepo....
git push temp master

This will create a repo with everything inside the server directory as the root.

  1. In your new repo
git subtree add -P packages/server /path/to/temp.git master

This will import the temp root into packages/server

dwjohnston
  • 11,163
  • 32
  • 99
  • 194