Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Procedure for cloning Git repos that use subtree

Tags:

git

subtree

I'm using Git's subtree command to pull a couple of libraries in to a project.

If I then clone the project in the normal way, I end up with all the code that I need, but I lose the subtree relationships - in the clone there is no remote for each of the libraries, and there is no -push branch for either of them.

What's the best way to re-establish this connection?

Is it sufficient to do

git remote add <lib> <remote-url>
git fetch <lib>

If I was adding the library for the first time I'd follow that with:

git subtree add -P <local/lib> --squash "<lib>/master"

This doesn't work when the local directory already exists though, which of course it will when you've cloned a project that has already had the library added to it.

Is there anything else that one should do in this situation, to ensure that subsequent git subtree merge and git subtree split commands to the expected thing?

like image 431
Sam Deane Avatar asked Jul 08 '10 10:07

Sam Deane


People also ask

How do I use Subtrees in git?

Specify 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 subtree split?

Splitting the Original Repository The subtree commands effectively take a folder and split to another repository. Everything you want in the subtree repo will need to be in the same folder.


2 Answers

I had very similar issue

Here is how I created the subtree at first

git remote add -f sub_project url_to_remote_repository/project.git
git merge -s ours --no-commit sub_project/master
git read-tree --prefix=sub/project/ -u sub_project/master
git commit -m "Added subtree merged in sub/project"

and to get changes from the "project" repo, I was doing

git pull -s subtree sub_project master

Now, I pushed my local repository to github and from another machine I cloned my github repository At that point, I got all the expected file in sub/project ... so that's great. But no more remote, and relation with sub/project. So I did the following

git remote add -f sub_project url_to_remote_repository/project.git
git merge -s ours --no-commit --squash sub_project/master
git pull -s subtree sub_project master

And that worked fine. Now I am able to manage the subtree from that cloned repository as before

git pull -s subtree sub_project master

Note:

1) in our project, before we were using git submodules which was really not good for our users. That's why we switched to git 's subtree system.
2) I had some weird errors when doing those operations on Windows machine (using git version 1.7.9.msysgit.0 ), and the same operations were successfully on linux. So now, to manipulate the subtree, I often use linux (and if I have any error, I try the same on linux).

Hope this can help.

like image 110
Jocelyn Avatar answered Sep 19 '22 18:09

Jocelyn


The way that I have in the past re-created that relationship was by doing a subtree merge.

git pull -s subtree <lib> master

even if there is nothing to merge in/pull it should simply return without doing anything. Feel free to add --squash to the above pull so that you don't pull in any remote history.

like image 44
X-Istence Avatar answered Sep 23 '22 18:09

X-Istence