Git-subtree: push changes from cloned repo

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

thanks in advance

+8
git git-subtree
source share
2 answers

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

If you use only git subtree push , you do not give the subtree enough information to extract and modify your changes.

If you cloned the repo correctly, the subtree will already be there. Subtree needs to be told which subtree you want to click on (even if you have one), and it also needs to know where to click - in particular, you don't want to click on the top-level repo. Therefore, you want something like:

 git subtree push --prefix=lib git@github.com:arges-github/lib.git master 

Obviously, repo and refspec should be changed to suit your repo.

If you want to see what happens here (and it helps), then the subtree actually splits the changes that affect the files inside the subtree to another branch, and then pushes them into the subtree repository. For this to happen, use subtree split

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

then look at the branch you made:

 git checkout lib-changes 

and, click them manually

 git push git@github.com:arges-github/lib.git master 

If this does not work, you may not have combined the subtree into your repo. When you add a subtree:

  git subtree add --squash --prefix lib git@github.com:arges-github/lib.git master 

you also need to merge the subtree and return it back to the top-level repository.

  git subtree pull --squash --prefix lib git@github.com:arges-github/lib.git master git push 
+14
source share

I had the same problem you encountered, and I solved it using the approach suggested by Roger Nolan. However, if you are not lucky enough to be case-insensitive in the file system, like me, you also need to make sure that every time you pull and push, the case of your prefix remains the same.

When you end up confusing the case by mistake, git will think that you have two subtrees, while there will be only one on the file system.

So, the solution I ended up with (in case this helps someone):

  • Create a script that will push your subtree. Name it publish_<your-subtree-name> .
  • Create another script that pulls out your subtree. Name it update_<your-subtree-name> .
  • Create a temporary branch in your HEAD and check for updates. If your script is right, your temp branch will not move.
  • Cloning the subtree repository elsewhere, add a test commit, click and try to translate it into your repo using the updated script that you just wrote.
  • If all this works, delete the temporary branch and now check both scenarios for superproject reprojection.
  • Then, when you need to click or pull subtrees, use scripts.

PS> The -squash parameter is important, especially if different branches of your repositories pull different branches of a subtree, and also if there are several subtrees in your project.

+1
source share

All Articles