For one of my hardware projects, I decided to try doing things a bit differently. Instead using a single repository, I decided to split it into two - one containing Firmware and other containing Hardware.
Since repository already had those as a subdirectories, I though using --subdirectory-filter
as recommended on GitHub would solve it all. Unfortunately, that left way too many commits not touching either of those files. So I decided to tweak procedure a bit.
I first removed all the files I didn’t need using --index-filter
. On that cleaned-up state I applied --subdirectory-filter
just to bring directory to the root. Unfortunately, while preserving tags was possible, it proved to be annoying enough to actually remove them all and manually retag all once done.
As discussed above, on the COPY of original repository we first remove all files/directories that are NOT Hardware and then we essentially move Hardware
directory to the root level of newly reorganized repository.
git remote rm origin
git filter-branch --index-filter \
'git rm -rf --cached --ignore-unmatch .gitignore LICENSE.md PROTOCOL.md README.md Firmware/' \
--prune-empty --tag-name-filter cat -- --all
rm -Rf .git/logs .git/refs/original .git/refs/remotes .git/refs/tags
git filter-branch --prune-empty --subdirectory-filter Hardware main
rm -Rf .git/logs .git/refs/original
git gc --prune=all --aggressive
git log --pretty --graph
With Hardware
repository sorted, I did exactly the same process for Firmware with the new COPY of original repository, only changing the directory names.
git remote rm origin
git filter-branch --index-filter \
'git rm -rf --cached --ignore-unmatch .gitignore LICENSE.md PROTOCOL.md README.md Hardware/' \
--prune-empty --tag-name-filter cat -- --all
rm -Rf .git/logs .git/refs/original .git/refs/remotes .git/refs/tags
git filter-branch --prune-empty --subdirectory-filter Firmware main
rm -Rf .git/logs .git/refs/original
git gc --prune=all --aggressive
git log --pretty --graph
Once I got two repositories, it was easy enough to combine them. I personally love using subtrees but submodules have their audience too.