Skip to content

Instantly share code, notes, and snippets.

@GerHobbelt
Forked from moyaldror/remove_git_submodule.sh
Last active May 22, 2020 10:47
Show Gist options
  • Save GerHobbelt/5f084b559d3197c04e90dfd018a00ee6 to your computer and use it in GitHub Desktop.
Save GerHobbelt/5f084b559d3197c04e90dfd018a00ee6 to your computer and use it in GitHub Desktop.

Revisions

  1. GerHobbelt revised this gist May 22, 2020. 1 changed file with 14 additions and 3 deletions.
    17 changes: 14 additions & 3 deletions remove_git_submodule.sh
    Original file line number Diff line number Diff line change
    @@ -3,8 +3,8 @@
    # Work repo: https://gist.github.com/GerHobbelt/5f084b559d3197c04e90dfd018a00ee6
    #
    # Sources:
    # https://stackoverflow.com/a/16162000/1635910
    # https://gist.github.com/myusuf3/7f645819ded92bda6677
    # https://www.freecodecamp.org/forum/t/how-to-remove-a-submodule-in-git/13228
    # https://stackoverflow.com/questions/1260748/how-do-i-remove-a-submodule/1260982#1260982
    #
    if [ $# -ne 1 ]; then
    @@ -62,9 +62,20 @@ echo "### Remove submodule '$MODULE_NAME':"
    # show the commands we're executing so we can diagnose which one spit out what error messages:
    set -x

    git config -f .gitmodules --remove-section submodule.$MODULE_NAME
    git submodule deinit -f $MODULE_NAME
    git rm $MODULE_NAME
    git add .gitmodules
    set +x
    cat <<EOT
    Note:
    The next couple of git commands MAY complain. That's fine.
    They're here to make absolutely sure any lingering cruft
    in the git parent repo has been removed.
    EOT
    set -x
    git config -f .gitmodules --remove-section submodule.$MODULE_NAME
    git config -f $DOTGIT_PATH/config --remove-section submodule.$MODULE_NAME
    git rm $MODULE_NAME
    rm -rf $DOTGIT_PATH/modules/$MODULE_NAME
    rm -rf $MODULE_NAME
  2. GerHobbelt revised this gist May 22, 2020. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions remove_git_submodule.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,8 @@
    #!/bin/bash
    #
    # Work repo: https://gist.github.com/GerHobbelt/5f084b559d3197c04e90dfd018a00ee6
    #
    # Sources:
    # https://gist.github.com/myusuf3/7f645819ded92bda6677
    # https://www.freecodecamp.org/forum/t/how-to-remove-a-submodule-in-git/13228
    # https://stackoverflow.com/questions/1260748/how-do-i-remove-a-submodule/1260982#1260982
  3. GerHobbelt revised this gist May 22, 2020. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions remove_git_submodule.sh
    Original file line number Diff line number Diff line change
    @@ -55,14 +55,13 @@ fi
    #echo "module_name: '$MODULE_NAME'"
    #echo "dotgit_path: '$DOTGIT_PATH'"

    echo "Remove submodule '$MODULE_NAME':"
    echo "### Remove submodule '$MODULE_NAME':"
    # show the commands we're executing so we can diagnose which one spit out what error messages:
    set -x

    git config -f .gitmodules --remove-section submodule.$MODULE_NAME
    git add .gitmodules
    git config -f $DOTGIT_PATH/config --remove-section submodule.$MODULE_NAME
    git rm --cached $MODULE_NAME
    git rm $MODULE_NAME
    rm -rf $DOTGIT_PATH/modules/$MODULE_NAME
    rm -rf $MODULE_NAME
  4. GerHobbelt revised this gist May 22, 2020. 1 changed file with 59 additions and 9 deletions.
    68 changes: 59 additions & 9 deletions remove_git_submodule.sh
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,68 @@
    #!/bin/bash

    #
    # https://gist.github.com/myusuf3/7f645819ded92bda6677
    # https://www.freecodecamp.org/forum/t/how-to-remove-a-submodule-in-git/13228
    # https://stackoverflow.com/questions/1260748/how-do-i-remove-a-submodule/1260982#1260982
    #
    if [ $# -ne 1 ]; then
    echo "Usage: $0 <submodule full name>"
    cat <<EOT
    Usage: $(basename $0) <submodule full name>
    You must specify the relative path to the submodule as the commandline parameter.
    The script will strip off the optional ./ so you are free to use TAB keys in bash
    to help you construct the path quickly.
    Note:
    You must run this script from the base directory of the parent repository, which
    contains the submodule you want to remove. (If that repo is a submodule itself,
    this script will take cope anyhow.)
    In other words: there MUST be a valid .git directory or .git file in your PWD!
    EOT
    exit 1
    fi

    MODULE_NAME=$1
    MODULE_NAME_FOR_SED=$(echo $MODULE_NAME | sed -e 's/\//\\\//g')
    # strip off ./, ../ and / absolute path prefixes, also strip off / at end of path:
    # let the checks below fail on the resulting relative path, which must exist
    MODULE_NAME=$( echo $1 | sed -e 's/^\.*\/\+//' -e 's/\/$//' )

    if [ -z "$MODULE_NAME" ] ; then
    echo "You must specify a valid, non-empty submodule path. Aborting."
    exit 1
    fi

    if ! [ -d $MODULE_NAME ] ; then
    echo "The submodule relative path '$MODULE_NAME' you specified does not exist. Aborting."
    exit 1
    fi

    if test -f .git ; then
    DOTGIT_PATH=$( grep 'gitdir:' .git | sed -e 's/gitdir: //' )
    elif test -d .git ; then
    DOTGIT_PATH=.git
    else
    echo "You are not invoking this from a git repo base directory. Aborting."
    exit 1
    fi

    if ! test -f .gitmodules ; then
    echo "There is no .gitmodules in your PWD. Aborting."
    exit 1
    fi

    #echo "module_name: '$MODULE_NAME'"
    #echo "dotgit_path: '$DOTGIT_PATH'"

    echo "Remove submodule '$MODULE_NAME':"
    # show the commands we're executing so we can diagnose which one spit out what error messages:
    set -x

    cat .gitmodules | sed -ne "/^\[submodule \"$MODULE_NAME_FOR_SED\"/,/^\[submodule/!p" > .gitmodules.tmp
    mv .gitmodules.tmp .gitmodules
    git config -f .gitmodules --remove-section submodule.$MODULE_NAME
    git add .gitmodules
    cat .git/config | sed -ne "/^\[submodule \"$MODULE_NAME_FOR_SED\"/,/^\[submodule/!p" > .git/config.tmp
    mv .git/config.tmp .git/config
    git config -f $DOTGIT_PATH/config --remove-section submodule.$MODULE_NAME
    git rm --cached $MODULE_NAME
    rm -rf .git/modules/$MODULE_NAME
    git rm $MODULE_NAME
    rm -rf $DOTGIT_PATH/modules/$MODULE_NAME
    rm -rf $MODULE_NAME
  5. Dror Moyal renamed this gist Feb 14, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. Dror Moyal created this gist Feb 14, 2019.
    18 changes: 18 additions & 0 deletions .sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    #!/bin/bash

    if [ $# -ne 1 ]; then
    echo "Usage: $0 <submodule full name>"
    exit 1
    fi

    MODULE_NAME=$1
    MODULE_NAME_FOR_SED=$(echo $MODULE_NAME | sed -e 's/\//\\\//g')

    cat .gitmodules | sed -ne "/^\[submodule \"$MODULE_NAME_FOR_SED\"/,/^\[submodule/!p" > .gitmodules.tmp
    mv .gitmodules.tmp .gitmodules
    git add .gitmodules
    cat .git/config | sed -ne "/^\[submodule \"$MODULE_NAME_FOR_SED\"/,/^\[submodule/!p" > .git/config.tmp
    mv .git/config.tmp .git/config
    git rm --cached $MODULE_NAME
    rm -rf .git/modules/$MODULE_NAME
    rm -rf $MODULE_NAME