You may have several git repos that you want to combine into a single git repo. Here is how you can accomplish this using git subtree...
- Create a new git empty repo.
- Add a remote for each repo you want to include in the final repo
git remote add k8s-azure-terraform https://github.com/robandpdx/k8s-azure-terraform.git
. - Fetch the remote branches
git fetch k8s-azure-terraform
- Use git subtree add to pull in the repo
git subtree add --prefix k8s-azure-terraform k8s-azure-terraform main
- Push all tags
git push --tags
Repeat the above steps for all repos you intend to combine in the monorepo.
Create a new branch in the monorepo for each brach in the repo you are adding to the monorepo. Then use git subtree pull
to merge the branch of the same name into the newly created branch. You may need to resolve conflicts. I usually resolve the conflicts using --theirs
. It is important to note that this rebases the brances from main
, which is probably OK, but definitely worth noting.
git subtree pull --prefix=k8s-azure-terraform k8s-azure-terraform webhook-autoscaler
grep -lr '<<<<<<<' . | xargs git checkout --theirs
git add -A
git commit -m "resolved conflict using --theirs"
Then push all branches to origin...
for REF in $(git for-each-ref --format='%(refname)' refs/heads); do
BRANCH_NAME=${REF#refs/heads/}
git push -u origin ${BRANCH_NAME}
done