Procedure for cloning Git repos that use 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 th开发者_高级运维is situation, to ensure that subsequent git subtree merge and git subtree split commands to the expected thing?
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.
I ran across this question while migrating a project from using submodules to subtrees and ended up using neither (at least not directly, anyway) - I am now using the git-subrepo tool.
It solves this problem by adding a .gitrepo file at the root of each subtree. The .gitrepo file contains tracking information, including the remote URL, and is then included in subsequent clones. This really simplifies things and the only complication is that git-subrepo has to be separately installed on each development machine (though the repository is still usable without it).
精彩评论