Skip to content
AndrewRichardSmart edited this page Aug 11, 2016 · 31 revisions

Q: How do I create a subrepo from a folder in old/existing work? (i.e. split from git subtree)

A: First cd into your local copy of that work. You have two options, either:

  1. You may put the <subdir> into its own separate (remote) repository (with optional unique branch name):
$ cd <old-project>
$ git subrepo init <subdir> -r <remote> [-b <branch>]
$ git subrepo push <subdir>

This is likely what you want, as the shared code will then be in its own repository. You may then use this as a subrepo in your new project via the git subrepo clone command:

$ cd <new-project>
$ git subrepo clone <remote> [<subdir>] [-b <branch>]    # Use <remote>, <branch> as above- where you had put the shared code. <subdir> is where you want the subrepo in this new project.
  1. Alternatively we can keep the <subdir> in its own branch in the original repository:
$ cd <old-project>
$ git subrepo init <subdir>
$ git subrepo branch <subdir>
$ git branch
* master
  subrepo/<subdir>    <--This is the branch containing <subdir> and all its history

That branch can be pushed/pulled wherever you need. This might be a bit cleaner for you in your use case. You may then use this as a subrepo in your new project via the git subrepo clone command:

$ cd <new-project>
$ git subrepo clone <old-project> [<subdir>] [-b 'subrepo/<subdir>']    # Use the correct branch name shown above.

Do whichever makes the most sense for your shared code as it pertains to your use case.

To migrate changes you've made back to that old project, you may either use git push, git subrepo push, or git subrepo pull depending on the context.

Q: How do I change tracking branch?

A: If you want to switch tracking branch on one of your subrepos, say go to another tag use:

git subrepo clone --force -b <new_branch> <subrepo>

This will re clone your subrepo and make it track another branch. Note that if you do this changes that you haven't pushed to the subrepo will only be left in the main repo.

Clone this wiki locally