Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Completely remove/delete git submodule from project

Completely Remove Git Sub-Module

Two different methods to automate the process of removing a submodule from a git repo with a single command. Adapted from here.

NOTE: A commit is made during removal, so it is best to commit any other changes prior to executing the command.

Using as a script in your PATH (filename will be name of command)

  • Create a new file anywhere in your shell's search paths (i.e. ~/.local/bin)
  • Copy the following
#!/usr/bin/bash
git rm "$1"
rm -rf ".git/modules/$1"
git config -f ".git/config" --remove-section "submodule.$1" 2> /dev/null
git commit -m "Remove submodule $1"
  • Make it executable chmod u+x ~/.local/bin/gitsubrm
  • Run it in terminal from the project root: gitsubrm relative/path/submodule

Using as a shell function

  • Somewhere in a file that gets sources by your shell (i.e. ~/.bashrc)
  • Copy the following
gitsubrm() {
    git rm "$1"
    rm -rf ".git/modules/$1"
    git config -f ".git/config" --remove-section "submodule.$1" 2> /dev/null
    git commit -m "Remove submodule $1"
}
  • Run it in terminal from the project root: gitsubrm relative/path/submodule (after shell restart/re-source)
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.