Git 如何删除子模块?

Git 如何删除子模块?,git,git-submodules,Git,Git Submodules,如何删除Git子模块 顺便问一下,有什么原因我不能这么做吗 git子模块rm whatever?必须删除.gitmodules和.git/config中的条目,并从历史记录中删除模块目录: git rm --cached path/to/submodule 如果您要在git的邮件列表上书写,可能会有人为您编写一个shell脚本。通过页面: 要删除子模块,您需要: 从.gitmodules文件中删除相关部分 准备.gitmodules更改:git add.gitmodules 从.git/con

如何删除Git子模块

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

必须删除
.gitmodules
.git/config
中的条目,并从历史记录中删除模块目录:

git rm --cached path/to/submodule
如果您要在git的邮件列表上书写,可能会有人为您编写一个shell脚本。

通过页面:

要删除子模块,您需要:

  • .gitmodules
    文件中删除相关部分
  • 准备
    .gitmodules
    更改:
    git add.gitmodules
  • .git/config
    中删除相关部分
  • 从工作树和索引中删除子模块文件:
    git rm--cached path_to_submodule
    (无尾随斜杠)
  • 删除子模块的
    .git
    目录:
    rm-rf.git/modules/path\u to\u子模块
  • 提交更改:
    git Commit-m“删除的子模块”
  • 删除现在未跟踪的子模块文件:
    rm-rf路径到子模块

  • 另请参见:。

    简单步骤

  • 删除配置项:
    git config-f.git/config--删除节子模块。$submodulename

    git config-f.gitmodules--删除节子模块。$submodulename
  • 从索引中删除目录:
    git rm--cached$submodulepath
  • 承诺
  • 删除未使用的文件:
    rm-rf$submodulepath

    rm-rf.git/modules/$submodulename
  • 请注意:
    $submodulepath
    不包含前导或尾随斜杠

    背景

    当您添加
    git子模块时,它只将其添加到
    .gitmodules
    ,但 一旦执行了
    git子模块init
    ,它就会添加到
    .git/config

    因此,如果您希望删除模块,但能够快速恢复, 那么就这样做:

    git rm --cached $submodulepath
    git config -f .git/config --remove-section submodule.$submodulepath
    
    首先执行
    git-rebase-HEAD
    git-commit
    最后,如果你把它放在脚本中


    还可以看看。

    除了建议之外,我还必须
    rm-Rf.git/modules/path/to/submodule
    才能添加一个同名的新子模块(在我的例子中,我用原来的fork替换了fork)

    <,提交并推送了一个已经是Git存储库的文件夹(包含
    .Git
    ),您将无法编辑
    .gitmodules
    文件或
    .Git/config
    中的任何内容。是:


    ,我还删除了
    .git
    文件夹,然后再执行
    git add

    总结一下,这是您应该做的:

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

    path\u to\u submodule=path/to/submodule

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

    git config -f .gitmodules --remove-section submodule.$path_to_submodule
    
    git config-f.gitmodules——删除节子模块。$path\u to\u子模块

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

    git config -f .git/config --remove-section submodule.$path_to_submodule
    
    git config-f.git/config——删除节子模块。$path\u to\u子模块

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

    git rm——缓存的$path\u到\u子模块

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

    git add .gitmodules
    
    git add.gitmodules

  • 提交超级项目

    git commit -m "Remove submodule submodule_name"
    
    git commit-m“删除子模块子模块名称”

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

    rm -rf path_to_submodule
    
    rm -rf $path_to_submodule
    
    rm -rf .git/modules/$path_to_submodule
    
    rm-rf$路径到子模块

    rm-rf.git/modules/$path\u to\u子模块


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

    [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配置中,然后您可以执行:
    git rms path/to/submodule
    我必须进一步执行John Dou的步骤,并将
    cd
    放入子模块的目录中,然后删除git存储库:

    cd submodule
    rm -fr .git
    

    然后,我可以将这些文件作为父Git存储库的一部分提交,而无需对子模块进行旧的引用。

    2012年12月我目前正在做的事情(结合了大多数答案):

    自:

    一旦您用“
    git submodule init
    ”表示您对子模块的兴趣,就没有办法说“我不再对这个子模块感兴趣了” “
    git子模块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
    :在中提到了这一点,总结如下:

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


    git rm
    :请参阅:

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

    让“
    git rm
    ”帮助用户,不仅从工作树中删除子模块,而且从
    .gitmodules
    文件中删除“
    子模块。
    ”部分,并将两者都转移到后台


    git子模块deinit
    :它源于:

    通过“
    git submodule init
    ”,用户可以告诉git他们关心一个或多个子模块,并希望将其保存下来
    git rm lib/blah
    
    git submodule deinit lib/blah
    git rm lib/blah
    git config -f .gitmodules --remove-section submodule.lib/blah
    
    git rm -r the_submodule
    rm -rf .git/modules/the_submodule
    
    git config -f .git/config --remove-section submodule.the_submodule 2> /dev/null
    
    #!/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)
    
    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
    
    git config -f .gitmodules --remove-section "submodule.submodule_name"
    
    git add .gitmodules
    
    git submodule deinit -f "submodule_name"
    
    git rm --cached path_to_submodule
    
    rm -rf .git/modules/path_to_submodule
    
    git commit -m "Removed submodule <name>"
    
    rm -rf path_to_submodule
    
    # 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
    
    git submodule deinit <submodule-name>    
    git rm <submodule-name>
    
    git rm -f the_submodule
    rm -rf .git/modules/the_submodule
    git config -f .git/config --remove-section submodule.the_submodule
    git commit -m "..."
    
    fatal: Not a git repository: 'the_submodule/.git'
    * the_submodule 73f0d1d...0000000:
    
    git submodule deinit -f {module_name}
    git add {module_name}
    git commit
    
    #!/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
    
    
    submodule="path/to/sub"              # no trailing slash!
    git submodule deinit -- "$submodule"
    git rm -- "$submodule"
    
    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 config --add url.NEWURLPREFIX.insteadOf ORIGINALURLPREFIX
    
    git config --add 'url./mnt/usb/repo/.insteadof' https://github.com/
    
    https://github.com/XXX/YYY.git
    
    /mnt/usb/repo/XXX/YYY.git
    
    $ 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'
    
    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 submodule add --name someunusedname https://github.com/hilbix/src.git two
    
    [submodule "someunusedname"]
        path = two
        url = https://github.com/hilbix/src.git
    
    someunusedname/
    two/
    
    path_to_submodule=path/to/submodule
    
    git config -f .gitmodules --remove-section submodule.$path_to_submodule
    
    git config -f .git/config --remove-section submodule.$path_to_submodule
    
    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
    
    git submodule deinit -f -- a/submodule    
    git rm -f a/submodule
    git commit