Skip to content

Instantly share code, notes, and snippets.

@ppsilv
Created January 19, 2025 02:47
Show Gist options
  • Select an option

  • Save ppsilv/8c6f87ce73a33301603b320efc138e82 to your computer and use it in GitHub Desktop.

Select an option

Save ppsilv/8c6f87ce73a33301603b320efc138e82 to your computer and use it in GitHub Desktop.
How to nest repos inside a repo.
Just to be clear, what you want is possible, if you're willing to make a
few compromises. First, we can bundle everything into a single repository.
Let's assume I have three existing repositories assignment1, assignment2,
and assignment3.
I can create an aggregate repository:
$ git init all-assignments
And then import the history of the three individual repositories into
this aggregate:
$ cd all-assignments
$ git remote add work1 .../url/to/assignment1
$ git remote add work2 .../url/to/assignment2
$ git remote add work3 .../url/to/assignment3
$ git remote update
And then run this script to create local branches from all the remote
branches:
git remote | while read remote; do
git for-each-ref --format='%(refname)' refs/remotes/$remote/ |
while read ref; do
name=$(sed "s|refs/remotes/$remote/||" <<<"$ref")
echo $name
git branch $remote-$name $ref
done
done
This gets us something like:
$ git branch
work1-master
work1-super-nifty-feature
work2-master
work2-update-before-final
work3-main
work3-quickstart
work3-hackathon
We don't have the file organization you wanted, but we do have the
repository histories all in a single place, and we can switch branches
as necessary to see the content.
If you really want all the files in the same place at the same time,
you can do something awful with git subtree. As above, add all the
remotes and run git remote update, and then:
git remote | while read remote; do
git for-each-ref --format='%(refname)' refs/remotes/$remote/ |
while read ref; do
name=$(sed "s|refs/remotes/$remote/||" <<<"$ref")
echo $name
git subtree add --prefix $remote/$name $ref
done
done
This results in:
.
├── work1
│ └── master
│ └── super-nifty-feature
├── work2
│ ├── master
│ └── update-before-final
└── work3
├── main
├── quickstart
└── hackathon
So now you have everything in one place, but it's all in the same branch,
and each former branch now lives in its own directory.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment