Make your existing a git folder a subtree

Following my Git subtree question of exporting and re-importing problems , I wanted to ask how to convert a folder to a subtree.

Imagine I have A repository where I have code that should now be shared with another project (and possibly more), so I put all the common code in a sub folder. This folder should now be extracted (without history, if possible) to expose the C repository. Subsequently, “sub” should become a subtree of C, but without losing the history in (very important). I also want to be able to make changes to "sub" and push them to C.

+6
source share
1 answer

git subtree split does just that. Given a folder through --prefix , it will generate a separate tree inside your repo, which you can click on to another repository and then use as you like.

The current version of git (1.8.1) does not include subtree documentation, but you can find it here: https://github.com/apenwarr/git-subtree/blob/master/git-subtree.txt

You can use the stream:

 # In Repo A create your subtree split and push it > git subtree split --prefix sub --branch subBranch > git push C subBranch:master # After some changes that touched the sub directory > git subtree push --prefix sub C master 

The last wil command re-partition the tree (using previously split commits to preserve the integrity of the C tree) and click on C/master .

If you want to delete history in content pressed in C , you can use the --squash option when git split -ing and git push -ing. You must be consistent and continue to use it in the future, because if you start mixing crushed and non-crushed subtree of content, you will not be able to correctly split and reuse previos distributions, and therefore, you will not be able to press it on C

Finally, once you have created the first sub split, it is not necessary that you delete it, and reimport as a new subtree. If you save it, git subtree will simply regenerate / reuse previously created commits from other sections. If you delete the folder and then add sub from the generated commits, it should still work, and if you use --squash , it will match what you want if C has no history, but keeping A history).

+7
source

Source: https://habr.com/ru/post/926242/


All Articles