代码之家  ›  专栏  ›  技术社区  ›  R. Martinho Fernandes

如何删除子模块?

  •  3145
  • R. Martinho Fernandes  · 技术社区  · 16 年前

    如何删除Git子模块?

    顺便问一下,有什么原因我不能这么做吗 git submodule rm whatever ?

    21 回复  |  直到 7 年前
        1
  •  3641
  •   John Douthat    7 年前

    通过网页 Git Submodule Tutorial

    1. 从列表中删除相关部分 .gitmodules 文件
    2. .git模块 变化:
      git add .gitmodules
    3. 从中删除相关部分 .git/config
    4. 从工作树和索引中删除子模块文件:
      git rm --cached path_to_submodule (无尾随斜杠)。
    5. 移除子模块的 .git 目录:
      rm -rf .git/modules/path_to_submodule
    6. 提交更改:
      git commit -m "Removed submodule <name>"
    7. 删除现在未跟踪的子模块文件:
      rm -rf path_to_submodule

    : alternative steps below .

        2
  •  2527
  •   Błażej Michalik Arkaprova Majumder    5 年前

    自从 git1.8.3 (April 22d, 2013) :

    一旦你表达了对某个子模块的兴趣,就没有办法说“我不再对这个子模块感兴趣了” git submodule init ".
    git submodule deinit “是这样做的方法。

    删除过程还使用 git rm (自2013年10月1.8.5日起)。

    总结

    0. mv a/submodule a/submodule_tmp
    
    1. git submodule deinit -f -- a/submodule    
    2. rm -rf .git/modules/a/submodule
    3. git rm -f a/submodule
    # Note: a/submodule (no trailing slash)
    
    # or, if you want to leave it in your working tree and have done step 0
    3.   git rm --cached a/submodule
    3bis mv a/submodule_tmp a/submodule
    

    解释

    rm -rf :这一点在 Daniel Schroeder answer ,并由 Eonil 在里面 the comments :

    这只剩下 .git/modules/<path-to-submodule>/ 不变。
    所以,如果您使用此方法删除一个子模块,然后再次添加它们,则将无法执行此操作,因为存储库已损坏。


    移除文件 commit 95c16418 :

    目前正在使用“ 移除文件 “在子模块上,从超级项目的工作树中删除子模块的工作树,从索引中删除gitlink。
    但是子模块的部分在 .gitmodules 保持不变,这是现在删除的子模块的剩余部分,可能会激怒用户(与中的设置相反) .git/config ,这必须作为一个提醒,提醒用户对此子模块感兴趣,以便稍后在签出旧提交时重新填充该子模块)。

    移除文件 submodule.<submodule name> “来自 .git模块


    :它源于 this patch

    有“ 初始化子模块 “用户可以告诉git他们关心一个或多个子模块,并希望在下次调用时填充它” git submodule update ".
    但目前,他们无法告诉git他们不再关心子模块,希望摆脱本地工作树(除非用户非常了解子模块的内部结构并删除“ submodule.$name.url “从 .git/config

    deinit
    移除整个 submodule.<name> 节自 对于给定的 . '已给出)。
    除非强制,否则如果当前工作树包含修改,则失败。
    当在命令行上给定的子模块中找不到url设置时进行投诉 ,但无论如何不要失败。

    这需要注意(反)初始化步骤是否正确( .git/modules/xxx )

    自git1.8.5以来 移除文件 照顾:

    • add '将子模块的url记录在 文件:需要为您删除它。
    • 子模块 special entry (如图所示) this question ):git rm将其从索引中删除:
      git rm --cached path_to_submodule
      这将删除以特殊模式“160000”存储在索引中的目录,并将其标记为子模块根目录。

    如果忘记了最后一步,并尝试将子模块添加为常规目录,则会收到如下错误消息:

    git add mysubmodule/file.txt 
    Path 'mysubmodule/file.txt' is in submodule 'mysubmodule'
    

    注意:自Git 2.17(2018年第2季度)以来,Git子模块Denit不再是shell脚本。
    这是对C函数的调用。

    看见 commit 2e61273 , commit 1342476 (2018年1月14日)由 Prathamesh Chavan ( pratham-pc )
    (由合并) Junio C Hamano -- gitster -- commit ead8dbe ,2018年2月13日)

    git ${wt_prefix:+-C "$wt_prefix"} submodule--helper deinit \
      ${GIT_QUIET:+--quiet} \
      ${prefix:+--prefix "$prefix"} \
      ${force:+--force} \
      ${deinit_all:+--all} "$@"
    
        3
  •  702
  •   fvgs    9 年前

    使用git 1.7.8或更新版本克隆的子模块在本地repo中最多会留下四条自身痕迹。以下三个命令给出了删除这四条记录道的过程:

    # Remove the submodule entry from .git/config
    git submodule deinit -f path/to/submodule
    
    # Remove the submodule directory from the superproject's .git/modules directory
    rm -rf .git/modules/path/to/submodule
    
    # Remove the entry in .gitmodules and remove the submodule directory located at path/to/submodule
    git rm -f path/to/submodule
    
        4
  •  578
  •   Noel Yap    5 年前

    git rm -r the_submodule
    rm -rf .git/modules/the_submodule
    

    正如@Mark Cheverton的回答正确指出的那样,如果第二行没有被使用,即使您现在删除了子模块,剩余的.git/modules/the_submodule文件夹也会阻止相同的子模块在将来被重新添加或替换。另外,正如@VonC所提到的, git rm 将在子模块上完成大部分工作。

    --更新(07/05/2017)--

    只是想澄清一下,, the_submodule 是项目内子模块的相对路径。例如,它是 subdir/my_submodule 如果子模块位于子目录中 subdir .

    正如评论和报告中正确指出的那样 other answers ,这两个命令(虽然功能上足以删除子模块)在 [submodule "the_submodule"] 部分 .git/config (截至2017年7月),可使用第三个命令删除:

    git config -f .git/config --remove-section submodule.the_submodule 2> /dev/null
    
        5
  •  225
  •   errordeveloper    9 年前


    1. git config -f .git/config --remove-section submodule.$submodulename
      git config -f .gitmodules --remove-section submodule.$submodulename
    2. 从索引中删除目录:
      git rm --cached $submodulepath
    3. 犯罪
    4. 删除未使用的文件:
      rm -rf $submodulepath
      rm -rf .git/modules/$submodulename

    $submodulepath 不包含前导或尾随斜杠。

    当你这样做的时候 git submodule add .gitmodules 但是 git submodule init .git/config .

    因此,如果您希望删除模块,但能够快速恢复,

    git rm --cached $submodulepath
    git config -f .git/config --remove-section submodule.$submodulepath
    

    这是一个好主意 git rebase HEAD 首先 git commit 最后,如果你把它放在脚本中。

    an answer to Can I unpopulate a Git submodule?

        6
  •  84
  •   GAMITG peaceman    10 年前

    除了建议之外,我还必须 rm -Rf .git/modules/path/to/submodule

        7
  •  67
  •   Doug    12 年前

    要删除使用添加的子模块,请执行以下操作:

    git submodule add blah@blah.com:repos/blah.git lib/blah
    

    运行:

    git rm lib/blah
    

    就这样。

    对于git的旧版本(约1.8.5),请使用:

    git submodule deinit lib/blah
    git rm lib/blah
    git config -f .gitmodules --remove-section submodule.lib/blah
    
        8
  •  55
  •   Carmine Paolino    14 年前

    .gitmodules .git/config

    git rm --cached path/to/submodule
    

    如果你写在git的邮件列表上,可能有人会为你编写一个shell脚本。

        9
  •  43
  •   GAMITG peaceman    10 年前

    总而言之,这是您应该做的:

    1. 设置 path_to_submodule

      path_to_submodule=path/to/submodule

    2. 从.gitmodules文件中删除相关行:

      git config -f .gitmodules --remove-section submodule.$path_to_submodule

    3. git config -f .git/config --remove-section submodule.$path_to_submodule

    4. git rm --cached $path_to_submodule

    5. 跟踪对.git模块所做的更改

      git add .gitmodules

    6. git commit -m "Remove submodule submodule_name"

    7. 删除现在未跟踪的子模块文件

      rm -rf $path_to_submodule

      rm -rf .git/modules/$path_to_submodule

        10
  •  43
  •   Community Mohan Dere    6 年前

    我发现 deinit

    git submodule deinit <submodule-name>    
    git rm <submodule-name>
    

    从…起 git docs :

    取消注册给定的子模块,即删除整个子模块 submodule.$name

        11
  •  42
  •   Charles    13 年前

    您可以使用别名自动执行其他人提供的解决方案:

    [alias]
      rms = "!f(){ git rm --cached \"$1\";rm -r \"$1\";git config -f .gitmodules --remove-section \"submodule.$1\";git config -f .git/config --remove-section \"submodule.$1\";git add .gitmodules; }; f"
    

    git rms path/to/submodule

        12
  •  42
  •   GAMITG peaceman    10 年前

    如果子模块是 意外地 添加是因为您添加、提交和推送了一个已经是Git存储库(包含)的文件夹 .git ),你不会有 .gitmodules .git/config In this case all you need 是:

    git rm --cached subfolder
    git add subfolder
    git commit -m "Enter message here"
    git push
    

    FWIW ,我还删除了 吉特先生 在执行以下操作之前,请先创建文件夹: git add .

        13
  •  21
  •   udondan    11 年前

    #!/bin/sh
    path="$1"
    if [ ! -f "$path/.git" ]; then
      echo "$path is no valid git submodule"
      exit 1
    fi
    git submodule deinit -f $path &&
    git rm --cached $path &&
    rm -rf .git/modules/$path &&
    rm -rf $path &&
    git reset HEAD .gitmodules &&
    git config -f .gitmodules --remove-section submodule.$path
    

    这将恢复与添加子模块之前完全相同的状态。您可以立即再次添加子模块,这在这里的大多数答案中是不可能的。

    git submodule add $giturl test
    aboveScript test
    

    这将为您提供一个干净的签出,无需提交任何更改。

    这是用以下方法测试的:

    $ git --version
    git version 1.9.3 (Apple Git-50)
    
        14
  •  18
  •   Lance Rushing    13 年前

    oldPath="vendor/example"
    git config -f .git/config --remove-section "submodule.${oldPath}"
    git config -f .gitmodules --remove-section "submodule.${oldPath}"
    git rm --cached "${oldPath}"
    rm -rf "${oldPath}"              ## remove src (optional)
    rm -rf ".git/modules/${oldPath}" ## cleanup gitdir (optional housekeeping)
    git add .gitmodules
    git commit -m "Removed ${oldPath}"
    
        15
  •  16
  •   Black    4 年前
    1. git submodule deinit <path to submodule>
    2. 从中删除该节 .gitmodules
    3. git rm <path to submodule>
    4. 删除需要从项目中删除的模块文件。
    5. 将删除的文件添加到git并调用 git add .gitmodules
    6. 提交并推送
        16
  •  15
  •   GAMITG peaceman    10 年前

    以下是我所做的:

    1.)从.gitmodules文件中删除相关部分。您可以使用以下命令:

    git config -f .gitmodules --remove-section "submodule.submodule_name"
    

    2)舞台表演 .gitmodules

    git add .gitmodules
    

    .git/config

    git submodule deinit -f "submodule_name"
    

    4.)删除gitlink(无尾随斜杠):

    git rm --cached path_to_submodule
    

    5.)清理垃圾 .git/modules

    rm -rf .git/modules/path_to_submodule
    

    6.)承诺:

    git commit -m "Removed submodule <name>"
    

    7.)删除现在未跟踪的子模块文件

    rm -rf path_to_submodule
    
        17
  •  14
  •   GAMITG peaceman    10 年前

    https://github.com/visionmedia/git-extras

    安装并键入:

    git-delete-submodule submodule
    

    然后事情就完成了。子模块目录将从repo中删除,并且仍然存在于文件系统中。然后,您可以提交更改,如下所示: git commit -am "Remove the submodule" .

        18
  •  11
  •   Peter Mortensen Pieter Jan Bonestroo    13 年前

    我不得不采取约翰·杜克的措施,这是一个更进一步的步骤 cd 进入子模块的目录,然后删除Git存储库:

    cd submodule
    rm -fr .git
    

        19
  •  9
  •   laser sheeraz ali    9 年前

    以下是我认为必要或有用的4个步骤(首先是重要步骤):

    git rm -f the_submodule
    rm -rf .git/modules/the_submodule
    git config -f .git/config --remove-section submodule.the_submodule
    git commit -m "..."
    

    理论上 git rm 在里面 第一步 你应该照顾好它。希望有一天,OP问题的第二部分能够得到肯定的回答(这可以在一个命令中完成)。

    但截至2017年7月, 是否需要删除中的数据 .git/modules/ 否则,您不能在将来重新添加子模块。

    tinlyx's answer 注意到 git submodule

    第3步删除的部分 the_submodule .git/config . 这是为了完整性。(对于较旧的git版本,该条目可能会导致问题,但我没有要测试的条目)。

    对于这一点,大多数答案建议使用 git submodule deinit . 我发现它更明确,使用起来也不那么混乱 git config -f .git/config --remove-section . 根据 git-submodule documentation , git deinit :

    来自存储库的子模块,并提交使用git rm[1] 相反 .

    最后但并非最不重要,如果你不 git commit ,执行此操作时将/可能会出错 git submodule summary (自git 2.7起):

    fatal: Not a git repository: 'the_submodule/.git'
    * the_submodule 73f0d1d...0000000:
    

    这与执行步骤2或步骤3无关。

        20
  •  9
  •   Tino    6 年前

    为了读者的利益,本文试图对其进行总结,并提供一个逐步的指南,说明如果事情没有按照预期的方式进行,该如何做。以下是 测试和安全的方式 对于 git 版本 2.17 及以上 :

    submodule="path/to/sub"              # no trailing slash!
    git submodule deinit -- "$submodule"
    git rm -- "$submodule"
    
    • 如果这不适用于您,请参阅下文。
    • 用Debian Buster测试 2.20.1 和Ubuntu 18.04 2.17.1 .
    • "$submodule" 只是强调名字应该放在哪里,你必须小心空格之类的
    • 如果在Windows上,则忽略第一行并替换 “$submodule” 使用Windows方式正确指定子模块的路径。(我不是Windows)

    切勿触摸机器内部 .git 你自己登记吧! 内部编辑 吉特先生 进入黑暗面。不惜一切代价远离!

    是的,你可以责怪我 吉特 对于这一点,因为许多方便的东西都丢失了 过去。就像一个正确的方法来移除子模块一样。

    git submodule . $GIT_DIR/modules/<name>/ 你自己 据我所知,这不仅是完全错误的,而且是极其危险的,而且会在未来引发严重的头痛! 见下文。

    注意

    git module deinit
    

    与之成反比吗

    git module init
    

    git submodule deinit -- module
    git rm -- module
    

    git submodule add -- URL module
    git submodule update --init --recursive -- module
    

    因为有些命令基本上需要做的不仅仅是一件事:

    • git submodule deinit -- module
      • (1) 更新 .git/config
    • git rm
      • (3) 从而递归地移除子模块的子模块
      • (4) 更新 .gitmodules
    • git submodule add
      • 将数据拉入到 .git/modules/NAME/
      • (1) 做 git submodule init ,所以更新 .git/config
      • (2) 做 git submodule update ,因此,非递归地签出模块
      • (4) 更新 .git模块
    • git submodule update --init --recursive -- module
      • (3) 递归地签出子模块的子模块

    这不能是完全对称的,因为保持严格对称没有多大意义。根本不需要两个以上的命令。另外,“拉入数据”是隐式的,因为您需要它,但是删除缓存信息是不需要的,因为这根本不需要,而且可能会删除宝贵的数据。

    这确实让新来者感到困惑,但基本上是件好事: 吉特 只是做了很明显的事情并且做得很好,甚至没有尝试做更多。 吉特 是一种工具,它必须做可靠的工作,而不仅仅是另一种“Eierlegende Wollmilchsau”(“Eierlegende Wollmilchsau”对我来说翻译为“瑞士军刀的邪恶版本”)。

    这是因为这里的“明显”取决于观点。在每种情况下,可靠性更为重要。因此,在所有可能的技术情况下,对您来说明显的东西往往不是正确的。请记住:AFAICS 吉特 遵循技术路线,而不是社会路线。(因此有了一个聪明的名字:git)

    如果失败了

    • 你的 他太老了。然后用新的 吉特 . (请参见下面的操作方法。)
    • 您的子模块在中不是干净的 git clean 感觉然后首先使用该命令清理子模块。(见下文。)
    • 你过去做过一些不受支持的事情 吉特 . 然后你就在黑暗面,事情变得丑陋和复杂。(也许使用另一台机器可以修复它。)
    • 也许还有更多我不知道的失败方式(我只是其中的一部分) 超级用户。)

    可能的解决办法如下。

    使用更新的 吉特

    如果您的机器太旧,则没有 submodule deinit 在你的 吉特 . 如果您不想(或可以)更新您的 ,然后只需使用另一台具有更新版本的机器 吉特 ! 吉特 要完成工作:

    • workhorse:~/path/to/worktree$ git status --porcelain 不能
    • workhorse:~/path/to/worktree$ ssh account@othermachine
    • othermachine:~$ git clone --recursive me@workhorse path/to/worktree/.git TMPWORK && cd TMPWORK
    • 现在做子模块的事情
    • othermachine:~/TMPWORK$ git commit . -m . && exit
    • workhorse:~/path/to/worktree$ git fetch account@othermachine:TMPWORK/.git
    • workhorse:~/path/to/worktree$ git merge --ff-only FETCH_HEAD . 如果这不起作用,请使用 git reset --soft FETCH_HEAD
    • 现在清理东西,直到 git status 他又干净了。你能够做到这一点,因为你已经有了清洁之前,感谢第一步。

    othermachine 可以是一些VM,或者Windows下的一些Ubuntu WSL,随便什么。甚至是 chroot (但我假设你是非root,因为如果你是 root 更新到新版本应该更容易 吉特 ).

    请注意,如果您不能 ssh 在中国,有很多运输方式 存储库。您可以将工作树复制到一些U盘上(包括

    git config --add url.NEWURLPREFIX.insteadOf ORIGINALURLPREFIX
    

    您可以使用此乘法,并将其保存到 $HOME/.gitconfig

    git config --add 'url./mnt/usb/repo/.insteadof' https://github.com/
    

    像这样重写URL

    https://github.com/XXX/YYY.git
    

    进入

    /mnt/usb/repo/XXX/YYY.git
    

    如果你开始习惯于强大的力量,这很容易 吉特

    先清理东西

    • 如果git抱怨未保存的东西,提交并将其推到安全的地方。
    • 如果git抱怨一些剩菜, git状态 git clean -ixfd 他是你的朋友吗
    • 试着放弃选择 rm deinit -f )为了 吉特 submodule 地区所以,安全总比后悔好。

    例子:

    $ git status --porcelain
     M two
    $ git submodule deinit two
    error: the following file has local modifications:
        two
    (use --cached to keep the file, or -f to force removal)
    fatal: Submodule work tree 'two' contains local modifications; use '-f' to discard them
    $ cd two
    $ git submodule deinit --all
    error: the following file has local modifications:
        md5chk
    (use --cached to keep the file, or -f to force removal)
    fatal: Submodule work tree 'md5chk' contains local modifications; use '-f' to discard them
    $ cd md5chk
    $ git submodule deinit --all
    error: the following file has local modifications:
        tino
    (use --cached to keep the file, or -f to force removal)
    fatal: Submodule work tree 'tino' contains local modifications; use '-f' to discard them
    $ cd tino
    $ git status --porcelain
    ?? NEW
    $ git clean -i -f -d
    Would remove the following item:
      NEW
    *** Commands ***
        1: clean                2: filter by pattern    3: select by numbers    4: ask each
        5: quit                 6: help
    What now> 1
    Removing NEW
    $ cd ../../..
    $ git status --porcelain
    $ git submodule deinit two
    Cleared directory 'two'
    Submodule 'someunusedname' (https://github.com/hilbix/src.git) unregistered for path 'two'
    

    你看,没有 -f 需要 子模块脱硝 . 如果事情是干净的,在一个 吉特清洁 感觉还要注意 git clean -x 这是不需要的。 git submodule deinit 这通常是你想要的,但别忘了。有时被忽略的文件可能很珍贵,比如缓存的数据需要数小时到数天才能重新计算。

    为什么从不删除 $GIT_DIR/modules/<名称>/ ?

    人们可能希望删除缓存的存储库,因为他们害怕以后遇到问题。这是真的,但遇到那个“问题”才是解决它的正确方法!因为修复很容易,而且做得好,你将能够从此幸福地生活。这避免了比您自己删除数据时更麻烦的问题。

    mkdir tmptest &&
    cd tmptest &&
    git init &&
    git submodule add https://github.com/hilbix/empty.git two &&
    git commit -m . &&
    git submodule deinit two &&
    git rm two &&
    git commit -m . &&
    git submodule add https://github.com/hilbix/src.git two
    

    最后一行输出以下错误:

    A git directory for 'two' is found locally with remote(s):
      origin    https://github.com/hilbix/empty.git
    If you want to reuse this local git directory instead of cloning again from
      https://github.com/hilbix/src.git
    use the '--force' option. If the local git directory is not the correct repo
    or you are unsure what this means choose another name with the '--name' option.
    

    为什么会出现这种错误?因为 .git/modules/two/ https://github.com/hilbix/empty.git 现在将从其他事物中重新填充,即 https://github.com/hilbix/src.git https://github.com/hilbix/empty.git

    --name someunusedname

    git submodule add --name someunusedname https://github.com/hilbix/src.git two
    

    .git模块 那么看起来

    [submodule "someunusedname"]
        path = two
        url = https://github.com/hilbix/src.git
    

    ls -1p .git/modules/ 给予

    someunusedname/
    two/
    

    由于 two/

    • 这不仅适用于你。对于使用您的存储库的所有其他人来说也是如此。
    • 你不会失去历史。如果您忘记推送旧子模块的最新版本,您可以输入本地副本,以后再这样做。请注意,有人忘记推送某些子模块是很常见的(因为这是新来者的一个PITA,直到他们习惯了) 吉特 ).

    但是,如果您删除了缓存目录,两个不同的签出将相互碰撞,因为您不会使用 --name 选择,对吗?因此,每次签出时,您可能必须删除 .git/modules/<module>/ 目录一遍又一遍。这非常麻烦,很难使用类似 git bisect

    因此,保留此模块目录作为占位符是一个非常技术性的原因。建议删除以下内容的人 .git/modules/ 要么不知道更好,要么忘了告诉你,这使得强大的功能,如 二分查找 如果它与这样的子模块不兼容,则几乎不可能使用。

    另一个原因如上所示。看这张照片 ls . 你在那里看到了什么?

    嗯,模块的第二个变体 两个/ 不在 ,正在进行中 .git/modules/someunusedname/ git rm $module; rm -f .git/module/$module 你完全错了!你必须咨询 module/.git .git模块

    因此,不仅大多数其他答案落入了这个危险的陷阱, even very popular git extensions had this bug it's now fixed there )! 所以最好把你的手放在 .git/ 目录如果你不准确,你在做什么!

    从哲学的角度来看,抹杀历史永远是错误的! Except for quantum mechanics ,和往常一样,但这是完全不同的。

    仅供参考,你可能猜到了: hilbix

        21
  •  8
  •   GAMITG peaceman    10 年前

    我刚刚找到了.submodule(忘记确切名称)隐藏文件,它有一个列表。。。您可以通过这种方式逐个删除它们。我只有一个,所以我把它删掉了。很简单,但它可能会搞乱Git,因为我不知道子模块是否附加了任何内容。除了libetpan通常的升级问题外,到目前为止似乎还可以,但这(希望)是不相关的。

    注意到没有人发布手动擦除,所以添加

        22
  •  8
  •   Albert Tobac    7 年前

    在git 2.17及更高版本中,它只是:

    git submodule deinit -f {module_name}
    git add {module_name}
    git commit
    
        23
  •  8
  •   Yuri Pozniak    5 年前

    在GitV2.7.4中,简单的3个步骤工作得很好。

    git submodule deinit -f -- a/submodule    
    git rm -f a/submodule
    git commit
    
        24
  •  7
  •   user257319 user257319    11 年前
    project dir:     ~/foo_project/
    submodule:       ~/foo_project/lib/asubmodule
    - - - - - - - - - - - - - - - - - - - - - - - - -
    run:
      1.   cd ~/foo_project
      2.   git rm lib/asubmodule && 
              rm .git/modules/lib/asubmodule && 
                git submodule lib/asubmodule deinit --recursive --force
    
        25
  •  4
  •   botbot    10 年前

    如果您刚刚添加了子模块,例如,您只是添加了错误的子模块或将其添加到了错误的位置,只需执行以下操作 git stash 然后删除文件夹。这是假设添加子模块是您在最近的回购协议中唯一做的事情。

        26
  •  3
  •   Rahul Dapke    6 年前

    总而言之,这是您应该做的:

    将路径设置为子模块变量(无尾随斜杠):

    path_to_submodule=path/to/submodule
    

    从.gitmodules文件中删除相关行:

    git config -f .gitmodules --remove-section submodule.$path_to_submodule
    

    从.git/config中删除相关部分

    git config -f .git/config --remove-section submodule.$path_to_submodule
    

    仅从索引中取消显示并删除$path_to_子模块(以防止丢失信息)

    git rm --cached $path_to_submodule
    

    git add .gitmodules
    

    提交超级项目

    git commit -m "Remove submodule submodule_name"
    

    删除现在未跟踪的子模块文件

    rm -rf $path_to_submodule
    
    rm -rf .git/modules/$path_to_submodule
    

    另见: Alternative guide lines

        27
  •  1
  •   Techradar    7 年前

    我创建了一个bash脚本来简化删除过程。它还检查回购协议中是否有未保存的更改,并要求确认。 它已经在计算机上进行了测试 os x

    https://gist.github.com/fabifrank/cdc7e67fd194333760b060835ac0172f

        28
  •  1
  •   eQ19    6 年前

    以防你需要在 单行命令 使用bash脚本,如下所示:

    $ cd /path/to/your/repo && /bin/bash $HOME/remove_submodule.sh /path/to/the/submodule

    在中创建bash脚本文件 $HOME 指定的目录,即。 remove_submodule.sh :

    #!/bin/bash
    
    git config -f .gitmodules --remove-section submodule.$1
    git config -f .git/config --remove-section submodule.$1
    git rm --cached $1
    git add .gitmodules
    git commit -m "Remove submodule in $1"
    rm -rf $1
    rm -rf .git/modules/$1
    git push origin $(git rev-parse --abbrev-ref HEAD) --force --quiet
    
    
        29
  •  1
  •   Pooja Singh    4 年前

    这对我有用。上面的答案在终端上显示了这一点,没有发生其他事情

    'fatal: not removing 'demolibapp' recursively without -r'
    
    1. demolibapp 是我要删除的子模块名称
    2. git子模块deinit demolibapp
    3. rm-rf.git/modules/demolibapp
    4. git commit-m“删除额外的子模块”
    5. git推送
        30
  •  0
  •   rashok    7 年前

    在最新的git中,只需4个操作即可删除git子模块。

    • .gitmodules
    • git add .gitmodules
    • 删除子模块目录 git rm --cached <path_to_submodule>
    • 承诺 git commit -m "Removed submodule xxx"