How can I create a gitsubtree of an existing repository?

git, git-subtree, repository

Solution

You can add projectB as a subtree of projectA using vanilla `git` (you don't need `git subtree`).

cd projectA
git remote add projectB_remote git@github.com/projectB.git
git fetch projectB_remote
git checkout -b projectB_branch projectB_remote/master
git checkout master
git read-tree --prefix=projectB/ -u projectB_branch

Explanation

- Enter the local `projectA` repo.

- Add a new remote called `projectB_remote` with projectB's url.

- Fetch `projectB_remote` without merging.

- Create and checkout a `projectB_branch`; bring in the `projectB_remote/master` files.

- Return to `projectA/master`.

- Create a subtree in `projectA/master` that contains a checkout of the `projectB_branch`.

Resultant Directory Structure

projectA
    projectB
    other.txt
    project.txt
    A.txt
    files.txt

See http://www.git-scm.com/book/en/v1/Git-Tools-Subtree-Merging

Problem

I'm trying to create a gitsubtree of an existing repository, for example: -> projectA/projectB Project A is the parent, i want to add project B as a git subtree. git subtree -P projectB ssh://git@github.com/projectB.git master But it fails, and shows the following message: prefix 'projectB' already exists. I don't want to download all the repository again, I just want to add this directory to my gitsubtree. This directory project B isn't tracked by Project A git. thanks in advance

Original source