Home > Blockchain >  Maintaining multiple unrelated histories in one git repository
Maintaining multiple unrelated histories in one git repository

Time:04-26

My use case: I want to create several small example programs, and store them all in an examples/ repo. When someone checkouts examples/a, I would like them to be able to run git log and see only the commits relevant to a/ without the large number of unrelated commits for b/ c/ d/ and e/.

I understand that git submodule or git subtree can do this, but those all require separate repos and I don't think it is worth creating 50 different repos of one or two files each. I would like them to all live in one repo, but maintain different histories. Is this possible?

CodePudding user response:

Yes, absolutely. You can fetch or create any history structures you like in a repository. Easiest way to make a new empty branch in examples is

git worktree add examples/new $(git commit-tree -mstub.message `git mktree <&-`)

and amend the stub commit with your first set of content.

I also use this regularly to carry "incorporated submodules", utility code that's always required but shared with other projects, the git submodule command is distinctly optional but it's easy to finagle it into working with this setup, just tell it you've already cloned the upstream module, for e.g. a "utils" history carried in the utils branch I'll

git submodule init utils
git worktree add utils :utils

on clone, with

[submodule "utils"]
    path = utils
    url = ./
    branch = utils

in .gitmodules and that's the machinery all set up.

  • Related