14

I have a main repository with two subtrees in the "external/" folder.

When I clone the repository, it downloads all the files, including the code that I previously pushed after the Git subtree command.

I want to clone that main repository on another machine and recreate the subtree structure, but the Git subtree add command fails because the folder where I want to put the subtree in already contains the folder and files.

> git clone https://URL/<main>
> git remote add <component> https://URL/<component>
> cd <main>
> git subtree add --prefix external/<component> <component> master
ERROR: prefix 'external/<component>' already exists.

How do I solve this or, is there a better way to have the subtree structure recreated for new clone operations?

Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188
wakeful
  • 163
  • 1
  • 6

3 Answers3

2

The point of Git subtree is that you don't need to "recreate" anything in another location or on another machine. When you clone the repository, it already contains the subtree as normal files via normal commits!

If you just want to "refresh" or "update" the contents of the subtree, try pull.

git subtree pull --prefix external/<component> <component> master

If for some reason (and could you please clarify) you really do want to start over, you need to remove the subdirectory and then re-add the subtree.

git rm -fr external/<component>
git commit -m "Removing old subtree"
git subtree add --prefix external/<component> <component> master
Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188
1

I want to do exactly the same and totally understand why the existing two answers are not accepted, because the following question/problem is not solved:

I want to clone that main repository on another machine and recreate the subtree structure, but the Git subtree add command fails because the folder where I want to put the subtree in already contains the folder and files.

Well, the answer/solution is simple. You don't --

The git subtree add needs to be done only once to the git repo, during the first/initial import, and never needs to be done again. The detailed reason/explanation is here.

So in the cloned new main repository, if you want to push to subtree, just

  1. Redo the git remote add in the new repository that you've done in the old repo before, only once as the setup.
  2. Then push to subtree just as what you've been doing in the old repo before

git subtree push --prefix external/<component> <component> master

Viola! The git subtree can now be updated from the new location/repository as well.

xpt
  • 20,363
  • 37
  • 127
  • 216
0

The error you are receiving is because the substree already exists:

ERROR: prefix 'external/<component>' already exists.

This is because when cloning the main repo you also received the code from the subtree. When adding a Subtree to a repo you are committing the code from the external branch/commit-hash into your source repo; you are committing the source to your repo, not linking to an external repo. If/when you want to pull-in the code base to a newer commit from the source repo of the Subtree you can execute the command:

git subtree pull --prefix external/<component> <component> <branch>

That command will pull in HEAD from the specified repo & branch and places the code over top of the existing code in the /external/<comonent> path. After you execute this subtree command you will still need to do a git commit to update the code.

Key note about Git: a Subtree differs from a Submodule in that a Submodule creates a reference/shortcut/link in the source repo to another (external) repo. With a Submodule the developer cloning the repo must also have credentials/access to the submodule-repo in order to pull that linked code into the repo being cloned. A Subtree is an code commit to the root repo. So if you have access to the repo you are cloning, you don't need access to the external-repo referenced in the Subtree.

benhorgen
  • 1,928
  • 1
  • 33
  • 38