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?
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.
The –prefix parameter defines the root directory for the cloned repository, then add the remote url, the branch and let it squash the entire commit history (–squash). Git doesn't like uncommitted changes so make sure to stash/commit any existing changes before adding a new subtree.
This takes advantage of the fact you want to move to another repo, so we can extract the subtree, and then relocate it in separate steps. Use git subtree split to extract the files you want to the an intermediate branch in your repository (you have already done this). git subtree split -P lib3 -b new-branch.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With