How to un-submodule a Git submodule?

Viewed 122693

What are the best practices for un-submoduling a Git submodule, bringing all the code back into the core repository?

13 Answers

Based on VonC's answer, I have created a simple bash script that does this. The add at the end has to use wildcards otherwise it will undo the previous rm for the submodule itself. It's important to add the contents of the submodule directory, and not to name the directory itself in the add command.

In a file called git-integrate-submodule:

#!/usr/bin/env bash
mv "$1" "${1}_"
git submodule deinit "$1"
git rm "$1"
mv "${1}_" "$1"
git add "$1/**"

In main repo

  • git rm --cached [submodules_repo]
  • git commit -m "Submodules removed."
  • git push origin [master]

In submodules repo

  • rm -rf .git

Again main repo

  • git add [submodules_repo]
  • git add .
  • git commit -m "Submodules repo added into main."
  • git push origin [master]

Here's what I found best & simplest.

In submodule repo, from HEAD you want to merge into main repo:

  • git checkout -b "mergeMe"
  • mkdir "foo/bar/myLib/" (identical path as where you want the files on main repo)
  • git mv * "foo/bar/myLib/" (move all into path)
  • git commit -m "ready to merge into main"

Back in main repo after removing the submodule and clearing the path "foo/bar/myLib":

  • git merge --allow-unrelated-histories SubmoduleOriginRemote/mergeMe

boom done

histories preserved

no worries


Note this nearly identical to some other answers. But this assumes you own submodule repo. Also this makes it easy to get future upstream changes for submodule.

Related