Skip to content

Instantly share code, notes, and snippets.

@ForeverZer0
Last active February 18, 2020 13:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ForeverZer0/c409fe71946d963d53aa354ce8a5cc02 to your computer and use it in GitHub Desktop.
Save ForeverZer0/c409fe71946d963d53aa354ce8a5cc02 to your computer and use it in GitHub Desktop.
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