Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a gitsubtree of an existing repository?

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://[email protected]/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

like image 294
ricardotk Avatar asked Jul 24 '13 19:07

ricardotk


People also ask

How do I create a subtree in git?

Adding a subtreeSpecify the prefix local directory into which you want to pull the subtree. Specify the remote repository URL [of the subtree being pulled in] Specify the remote branch [of the subtree being pulled in] Specify you want to squash all the remote repository's [the subtree's] logs.

What is git sub tree?

git subtree lets you nest one repository inside another as a sub-directory. It is one of several ways Git projects can manage project dependencies. Why you may want to consider git subtree. Management of a simple workflow is easy.

What does git subtree push do?

git subtree split lets you specify a rev other than HEAD. ' git push '(man) lets you specify a mapping between a local thing and a remot ref. So smash those together, and have git subtree push let you specify which local thing to run split on and push the result of that split to the remote ref.


Video Answer


2 Answers

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 [email protected]/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

  1. Enter the local projectA repo.
  2. Add a new remote called projectB_remote with projectB's url.
  3. Fetch projectB_remote without merging.
  4. Create and checkout a projectB_branch; bring in the projectB_remote/master files.
  5. Return to projectA/master.
  6. 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

like image 95
Shaun Luttin Avatar answered Oct 31 '22 19:10

Shaun Luttin


When adding a subtree, it seems that the prefix (subdirectory in which you will add the subtree) cannot already exist.

I worked around this problem by checking-out to a commit before this subdirectory existed, doing the git subtree add, and then merging with the branch that contained my old subdirectory contents.

like image 23
Chris Peckham Avatar answered Oct 31 '22 20:10

Chris Peckham