Skip to content

Instantly share code, notes, and snippets.

@cruftlord
Forked from korya/Subfolder to git repo.md
Last active December 22, 2023 03:26
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save cruftlord/495a78ce33b6e4910585 to your computer and use it in GitHub Desktop.
Save cruftlord/495a78ce33b6e4910585 to your computer and use it in GitHub Desktop.
Convert subfolder into Git submodule

Source: http://willandorla.com/will/2011/01/convert-folder-into-git-submodule/

1. Clone new repository

$ git clone --no-hardlinks original-repo copied-repo

2. Filter out the files you want to keep and remove the others

$ cd copied-repo
$ git filter-branch --subdirectory-filter sub/module/path HEAD -- --all
$ git reset --hard
$ git gc --aggressive
$ git prune
$ git remote rm origin

3. Push the new repositories to the upstream server

$ git remote add origin git@github.com:user/submodule-repo.git

4. Add the new repository as submodules to the original repository

$ cd original-repo
$ git rm -r sub/module/path
$ git commit -m "Removing the folders that are now repositories"
$ rm -rf sub/module/path
$ git submodule add git@github.com:user/submodule-repo.git sub/module/path
$ git submodule init
$ git submodule update
$ git add .gitmodules sub/module/path
$ git commit -m "Added in submodules for removed folders"
@Kreijstal
Copy link

just use git submodule add $(git -C ./subdirectory config --get remote.origin.url) ./subdirectory assuming the subdirectory is already a git repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment