2

I recently moved a couple of subdirectories in a project of mine on GitHub, and I was curious about the best way to clone a subdirectory into a new project. Specifically, I want any mentions of issues in the commit messages for these new projects to point to the old project.

Stuart P. Bentley
  • 10,195
  • 10
  • 55
  • 84
  • 1
    possible duplicate of [Detach subdirectory into separate Git repository](http://stackoverflow.com/questions/359424/detach-subdirectory-into-separate-git-repository) – IQAndreas Aug 10 '14 at 08:58

1 Answers1

4

Despite hype around new commands like git subtree split, the best way to make a new Git project from a subdirectory of an old Git project is still to clone the project, then use git filter-branch --subdirectory-filter to rewrite the project's history to only include the given subdirectory.

However, with GitHub, there's still a little more work to do- if you're taking commits from an old project into a new repository, you may have references in your commit messages to issues from that original repository (such as Fixes #3). If you're going to re-use these commits in another project, you'll need to convert them to explicit references to issues in the original repository (such as Fixes stuartpb/mothership#3), so that the "new" commits in the separated project will be linked to that original issue, and won't get linked to any of the new project's issues.

Luckily, git filter-branch provides an option to filter commit messages as well, so we can do all this in just a few commands:

  • $WORK_DIR refers to the directory you want to check your project out to.
  • $SUB_DIR is the subdirectory you want to convert to a separate project.
  • $ORIGINAL_REPO is the username-and-repo-name of the project with the subdirectory you're splitting.
  • $NEW_REPO is the username-and-repo-name you want to push the new repo to.

The commands, from start to finish:

git clone https://github.com/$ORIGINAL_REPO $WORK_DIR && cd $WORK_DIR
git filter-branch --subdirectory-filter $SUB_DIR --msg-filter 'sed -re
  "s=(^| )(#[0-9]+)=\1'$ORIGINAL_REPO'#\2"=g' -- --all
git remote set-url origin git@github.com:$NEW_REPO.git
git push -u origin master
Community
  • 1
  • 1
Stuart P. Bentley
  • 10,195
  • 10
  • 55
  • 84
  • Why add a new question/answer instead of just adding your answer to [an existing question](http://stackoverflow.com/q/359424/617937)? – IQAndreas Aug 10 '14 at 08:59
  • 2
    Because my question/answer is GitHub-specific. Other questions are only about doing this for Git repositories in general. – Stuart P. Bentley Aug 10 '14 at 22:47
  • What people fail to understand is that Github '''is''' Git. The commands above are just how to do it from the console. – carlspring Aug 12 '14 at 08:10
  • Uh, "Git" doesn't have an issue tracker, which is what half the content of this answer pertains to addressing. – Stuart P. Bentley Aug 13 '14 at 09:38