DEV Community

Cover image for git and maybe monorepo
M
M

Posted on

git and maybe monorepo

hey,
after some googling about "monorepos" topic, i think i found something interesting that i wanted to share.

we will use git config and git subtree.

the 1st problem is how to not to loose repo, from which i found code ( submodules does that, but not subtree )

What i found is, that git knows how to to include another config into .git/config so repo could carry remotes around as text file in repo

git config --local include.path ../.gitconfig
Enter fullscreen mode Exit fullscreen mode

this command - adds ../.gitconfig to ./.git/config

than we can use custom alias from .gitconfig which adds remote repo to .gitconfig as "remote [origin] and calls git subtree command. Here is an example:

git rst path/to/subtree remote_or_url branch [--squash]
Enter fullscreen mode Exit fullscreen mode

this will create git remote add -f path/to/subtree remote_url and calls git subtree add --prefix path/to/subtree path/to/subtree [branch] [flags]

and the base of .gitconfig in repo:

# run this command to add a remote to the .gitconfig file
# > git config --local include.path ../.gitconfig
# > git rst path/to/subtree remote_or_url branch [--squash]

[alias]
     stpush = "!f() { a1=$1; a2=$2; git subtree push --prefix \"$a1\" \"$a1\" \"$a2\"; }; f"
     stpull = "!f() { a1=$1; a2=$2; git subtree pull --prefix \"$a1\" \"$a1\" \"$a2\"; }; f"
     rst = "!f() { a1=$1; a2=$2; shift;shift; a3=$@; git subtree add --prefix \"$a1\" \"$a2\" $a3; git config -f .gitconfig --add remote.\"$a1\".url \"$a2\"; git config -f .gitconfig --add remote.\"$a1\".fetch \"+refs/heads/*:refs/remotes/origin/*\"; }; f"

Enter fullscreen mode Exit fullscreen mode

Top comments (0)