git-subtree: Push changes from an cloned repo

arge picture arge · Oct 9, 2012 · Viewed 21.2k times · Source

I'm using git-subtree(from Avery Pennarun). In my current git repo I have of course all my project files/folders and a subtree called "lib". If I now clone this git repo using git clone I get all of the project files and the subtree "lib" (everything as it should be). What I tried now: I changed something within the subtree "lib" in the cloned repo and tried to push the changes back to the remote repo of the subtree "lib" using git subtree push, but it didn't work. What is the problem? Do I have to add it as subtree first with git subtree add?

Thx in advance

Answer

Rog picture Rog · Oct 10, 2012

Disclaimer, I suspect I am only a few days ahead of you learing about subtree :-)

If you are just using git subtree push you are not giving subtree enough information to extract and push your changes.

If you cloned the repo correctly the subtree will already be in there. Subtree needs to be told which subtree you want to push from (even if you only have one) and it also needs to know where to push to - specifically, you do not want to push to the top level repo. Hence, you want something like:

git subtree push --prefix=lib [email protected]:arges-github/lib.git master

Obviously the repo and refspec should be changed to match your repo.

If you want to look into what is happening here (and it does help) subtree actually splits the changes that affect the files inside the subtree into a different branch and then pushes that to the subtree repo. To see this happen, use subtree split

git subtree split --rejoin --branch=shared-changes --prefix=lib

then have a look at the branch you've made:

git checkout lib-changes

and, push them manually

git push [email protected]:arges-github/lib.git master

If this isn't working then it may be that you have not merged the subtree into your repo. When you add a subtree:

 git subtree add --squash --prefix lib [email protected]:arges-github/lib.git master

you also need to merge the subtree and push it back to your top level repo.

 git subtree pull --squash --prefix lib [email protected]:arges-github/lib.git master
 git push