Skip to content

Instantly share code, notes, and snippets.

@korya
Last active December 16, 2023 10:29
Show Gist options
  • Star 89 You must be signed in to star a gist
  • Fork 30 You must be signed in to fork a gist
  • Save korya/9047870 to your computer and use it in GitHub Desktop.
Save korya/9047870 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: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
Copy link

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
Copy link

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...

@mrcbax
Copy link

mrcbax commented May 1, 2016

misspelled submodule in step 4

@gcallah
Copy link

gcallah commented Sep 24, 2017

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

@gadebharath
Copy link

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
Copy link

dagobit commented Feb 4, 2020

$ git submoduel update
should be spelling fixed

@korya
Copy link
Author

korya commented Feb 4, 2020

Thanks, guys. I've fixed the typos.

@int-ua
Copy link

int-ua commented Dec 12, 2020

Alternative to filter-branch that is now recommended by the git itself:
https://github.com/newren/git-filter-repo/

git filter-repo --subdirectory-filter sub/module/path

But both of them lose track if the folder was moved at any commit. Solved: newren/git-filter-repo#182

@BrutalSimplicity
Copy link

Gold! Thanks

@lukipedio
Copy link

lukipedio commented Sep 23, 2021

Just note you missed the obvious 'git push --set-upstream origin master' command at point 3 :-)

@klovsb
Copy link

klovsb commented Aug 29, 2022

I am learning about submodule to see if it is a good method to maintain version of an open source application and almost 100 third party plugins. We don't modify the codes but we have a few instances of the application. Do I run this script for every plugin, which means I have a copy of all the plugin I use? Sorry I am still learning about Git. Thanks in advance.

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