I\'m looking for a way to set up git respositories that include subsets of files from a larger repository, and inherit the history from that main repository. My primary motivat
Let me first summarize your question:
From your stats I can see that you have 14 sub-projects stored in one master repository. This is usually a very poor solution because remember that every single time someone is cloning the repository, it will also get the full history of all the sub-projects. For instance If I want to contribute to one of your sub-project, I do not want to carry all the 8096 files you have.
If the projects are unrelated to each others, just split them into sub-repositories. With GitHub you can create organizations. Do not hesitate to create your own organization an put all your sub-projects into it. The main advantage is that each sub-project will have:
If you have related projects which each of them need to be taken from a particular commit. I recommend you to use git submodules. For example if you look at the TortoiseGit project in the ext/
folder, you will notice links to other repositories.
Another solution would be to use git subtree, which seem not the best solution for your problem.
If your master repository falls into any of these categories, you should review your way of using Git:
.exe
, .tmp
, binaries, generated files, .pdf
...)Is your repository public on GitHub?
As I understand your question
git subtree
or git submodules
One way to extract the history of just a subset of the files into a dedicated branch (which you then can push into a dedicated repository) is using git filter-branch
:
# regex to match the files included in this subproject, used below
file_list_regex='^subproject1/|^shared_file1$|^lib/shared_lib2$'
git checkout -b subproject1 # create new branch from current HEAD
git filter-branch --prune-empty \
--index-filter "git ls-files --cached | grep -v -E '$file_list_regex' | xargs -r git rm --cached" \
HEAD
This will
subproject1
based on the current HEAD
(git checkout -b subproject1
)git filter-branch [...] HEAD
)xargs -r git rm --cached
) that are not part of the subproject (git ls-files --cached | grep -v -E '$file_list_regex'
)--prune-empty
).--index-filter
/--cached
).This is a one-time operation though but as I understand your question you want to continously update the extracted subproject repositories/branches with new commit.
The good news is you could simply repeat this command since git filter-branch
will always produce the same commits/history for your subproject branches - given that you don't manually alter them or rewrite your master branch.
The drawback of this is that this would filter-branch
the complete history each time and for each subproject again and again.
Given that you only want to add the last 5 commits of the master
branch to the tip of your existing subproject1
branch you could adapt the commands like this:
# get the full commit ids for the commits we consider
# to be equivalent in master and subproject1 branch
common_base_commit="$(git rev-parse master~6)"
subproject_tip="$(git rev-parse subproject1)"
# checkout a detached HEAD so we don't change the master branch
git checkout --detach master
git filter-branch --prune-empty \
--index-filter "git ls-files --cached | grep -v -E '$file_list_regex' | xargs -r git rm --cached" \
--parent-filter "sed s/${common_base_commit}/${subproject_tip}/g" \
${common_base_commit}..HEAD
# force reset subproject1 branch to current HEAD
git branch -f subproject1
Explanation:
git filter-branch [...] ${common_base_commit}..HEAD
) up to master~6
which we consider to be the equivalent commit to subproject1
s current tip.master~6
to subproject1
(--parent-filter 'sed s/${common_base_commit}/${subproject_tip}/g'
) effectively rebasing the 5 rewritten commits on top of subproject1
.subproject1
to include the new commits on top of it.Further optimazation/automation:
$file_list_regex
) or actually to exclude (git ls-files --cached | grep -v -E '$file_list_regex'
) from a given subproject$GIT_COMMIT
) or check-in the list to the repository itself in case the files to include per subproject may change over timegit update-project subproject1
You are looking for git submodules:
It often happens that while working on one project, you need to use another project from within it. Perhaps it’s a library that a third party developed or that you’re developing separately and using in multiple parent projects. A common issue arises in these scenarios: you want to be able to treat the two projects as separate yet still be able to use one from within the other.
The TL;DR on submodules is that they are repos contained within other repos.
The only thing the parent repo knows about the child is the SHA of the last commit that the child told it about, so each repo is managed independent of the other, but they have references to each other which allows you to compose them together.
Here's a well-written blog post from GitHub on the topic.