Git subtree is divided into two directories

I followed this excellent answer to extract a subdirectory of my git repository into my own repository, while preserving the whole story.

My repository looks like this:

src/ http/ math/ tests/ http/ math/ 

I want to create a new branch that contains only the src/math and tests/math directories.

If I run the following command:

 git subtree split -P src/math -b math 

It creates a branch containing the contents of the src/math directory, but discards the src/math/ prefix.

If I try the same command with two directories:

 git subtree split -P src/math -P tests/math -b math 

It only extracts the contents of tests/math , ignoring src/math , and also discarding the tests/math prefix.

To summarize, I would like my last repository to look like this:

 src/ math/ tests/ math/ 

That is, preserving the original directory structure, but discarding everything that is not explicitly specified on the command line.

How can i do this?

+8
source share
2 answers

Depending on your needs, you can leave with git filter-branch .

I'm not quite sure what you are trying to achieve, but if you just want to delete a repository with two directories deleted (in history?), This is probably your best shot.

See also Rewriting Git History .

 $ git filter-branch --tree-filter 'rm -rf tests/http src/http' --prune-empty HEAD 

This will examine each commit and remove two directories from this commit. Keep in mind that this overwrites the story (i.e.: changes your team) and causes headaches if you have a common story with another repository.

+3
source

Use git-subtree add to split

 # First create two *split-out* branches cd /repos/repo-to-split git subtree split --prefix=src/math --branch=math-src git subtree split --prefix=test/math --branch=math-test # Now create the new repo mkdir /repos/math cd /repos/math git init # This approach has a gotcha: # You must commit something so "revision history begins", # or 'git subtree add' will complain about. # In this example, an empty '.gitignore' is commited. touch .gitignore git add .gitignore git commit -m "add empty .gitignore to allow using git-subtree" # Finally, *split-in* the two branches git subtree add --prefix=src/math ../repo-to-split math-src git subtree add --prefix=test/math ../repo-to-split math-test 

This worked for me with git --version 2.23.0 . Also note that you can set different prefixes during splitting, i.e. add src/math/ to src/ and test/math/ to test/ .

Additional note : use git log in the new repo before performing operations on the remote computer to see if the resulting history suits you. In my case, I have some duplicate message commits, because my repo history was so dirty, but for me it's ok.

A source

0
source

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


All Articles