Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a specific folder when using git subtree?

I have a complicated Ionic project I'm developing. Many of the components and providers I'm developing are generic and can be used in other projects my company is doing. This is common in software development. This is the Git workflow I have come up with (this graph shows branches):

my-company-library-repo ----
                            |_ component 1 feature branch
                               |_ company component 1 feature branch testbed
                            |_ component 2 feature branch

The final component code (just a .ts or .js file) developed in the testbed is pushed to component feature branch. The testbed code stays in the testbed branch. Also in the feature branch goes any docs that might accompany the component.

Now in the application repo I add the feature branch as a subtree using this command:

git subtree add -P <destination-dir/feature> --squash <my-company-library-repo-url> <feature-branch-name>

And that gave me the following (this graph shows folder structure):

my-app-repo-------
                  |_ company-library-feature-subtree

This should only contain the .js or .ts and it's docs in it's subfolder. I get this only to work part way. When it pulls the subtree, it does only pull the component and it's doc files, but the files get pulled into a very long list of sub-directories like this:

my-app-repo/src/feature-branch/feature/src/app/providers/...

This makes it hard to use the library because the files are put so many directories (unused directories) deep.

So, when I push my 2 files from the feature-testbed branch to the feature branch, how can I not pull that whole directory structure with them?

like image 953
juliet Avatar asked Oct 26 '16 15:10

juliet


People also ask

Can we checkout specific folder in git?

To checkout everything from your HEAD (not index) to a specific out directory: git --work-tree=/path/to/outputdir checkout HEAD -- . Save this answer.

How does git subtree work?

git subtree allows you to nest a repository as a subdirectory inside another. It's one of the various options for managing project dependencies in Git projects. You add a subtree to an existing repository where the subtree is a reference to another repository URL and branch/tag when you wish to utilize it.

How do I create a subtree in git?

Adding a subtreeSpecify 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.

What is git subtree split?

This takes advantage of the fact you want to move to another repo, so we can extract the subtree, and then relocate it in separate steps. Use git subtree split to extract the files you want to the an intermediate branch in your repository (you have already done this). git subtree split -P lib3 -b new-branch.


2 Answers

Before adding the subtree to my-app-repo, split a subtree from my-company-library-repo:

# In my-company-library-repo
git subtree split -P src/app/providers/... -b feature-new feature

This will create a new history with the contents of src/app/providers/... at the root of the repo, starting at the feature branch, and create the branch feature-new at the end of this history.

Then add that new branch as a subtree to my-app-repo:

# In my-app-repo
git subtree add -P <destination-dir/feature> --squash <my-company-library-repo> feature-new

Now you will have the contents of src/app/providers/... at <destination-dir/feature>.

You didn't mention whether you will be repeating this process regularly, but that is possible too. From the git-subtree man page:

Repeated splits of exactly the same history are guaranteed to be identical (ie. to produce the same commit ids). Because of this, if you add new commits and then re-split, the new commits will be attached as commits on top of the history you generated last time, so 'git merge' and friends will work as expected.

like image 150
Scott Weldon Avatar answered Oct 10 '22 03:10

Scott Weldon


I researched Scott Weldon's solution. It would work, but it seems git subtree splits the said directory into its own repo. At least that's what I gleaned from reading man pages and books and what not. (If I'm wrong, which may well be the case, please let me know.) That's not what I wanted to do.

I did, however, find the solution to my problem. Here's what I did using the Git Subtree Merge Strategy (instead of the GIT SUBTREE command) in my project:

$ git remote add my-library <my-library-url>
$ git fetch my-library
$ git checkout -b my-library-branch my-library/master
$ git checkout master
$ git read-tree --prefix=<desired/library/dir> -u my-library-branch
$ git commit -m "Merged library project as subdirectory"
$ git push

This worked great. I have my library in a reasonable subfolder. Admittedly, I have to take the whole library, not just a chunk like a custom component or provider, but that's OK in this case.

like image 22
juliet Avatar answered Oct 10 '22 02:10

juliet