Skip to content

Instantly share code, notes, and snippets.

@MJeorrett
Last active June 19, 2023 17:25
Show Gist options
  • Save MJeorrett/50b707ab49d05cebb15a117f7fd6a20a to your computer and use it in GitHub Desktop.
Save MJeorrett/50b707ab49d05cebb15a117f7fd6a20a to your computer and use it in GitHub Desktop.
Replace directory with git submodule

These are the steps required to replace a directory with a submodule in git. Note: Assumes that you are currently working on branch called 'develop'

  1. Check out a new branch to make the changes on: git checkout -b creating-submodule
  2. delete directory to be replaced rm -rf path/of/directory/to/be/replaced
  3. git add . then git commit -m "removing local directory"
  4. add the submodule: git submodule add "https://github.com/repoName" path/of/directory/to/be/replaced
  5. git add . then git commit -m "adding submodule"
  6. delete the submodule directory if you don't do this git will throw a hissy when you try to checkout out develop to merge the changes in
  7. git checkout develop
  8. pull the submodule back into the local repo: git submodule foreach git fetch --tags then git submodule update --init --recursive
  9. git push
@queglay
Copy link

queglay commented Nov 29, 2020

I found that in git version 2.24.3, after step 5 I only needed to git push.

@lukipedio
Copy link

lukipedio commented Sep 30, 2021

You probably miss a merge step after 7
git merge creating-submodule

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