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?
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.
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.
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.
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.
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