Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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:korya/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"
$ git submodule add git@github.com:korya/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"
@JacobAMason

This comment has been minimized.

Copy link

JacobAMason commented Jan 27, 2016

Step 3 should read $ git remote add origin git@github.com:korya/submodule-repo.git
The original source had this wrong too, I think.

@DreadKnight

This comment has been minimized.

Copy link

DreadKnight commented Feb 5, 2016

Isn't git subtree an easier/better way of doing things now? I'm not a git expert, so I'm trying to figure things out...

@LogoiLab

This comment has been minimized.

Copy link

LogoiLab commented May 1, 2016

misspelled submodule in step 4

@gcallah

This comment has been minimized.

Copy link

gcallah commented Sep 24, 2017

This reads like it was generated by the parody git documentation generator.

@gadebharath

This comment has been minimized.

Copy link

gadebharath commented Aug 9, 2019

Step 3 should read $ git remote add origin git@github.com:korya/submodule-repo.git
The original source had this wrong too, I think.

+1

@dagobit

This comment has been minimized.

Copy link

dagobit commented Feb 4, 2020

$ git submoduel update
should be spelling fixed

@korya

This comment has been minimized.

Copy link
Owner Author

korya commented Feb 4, 2020

Thanks, guys. I've fixed the typos.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.