git clean不会在切换分支时删除添加到分支的子模块

时间:2012-02-16 15:48:33

标签: git

切换分支时如何摆脱子模块。我不明白为什么git clean说它删除了子模块但没有。这是一个错误吗?下面是剪切和粘贴步骤以重现。

git --version
git version 1.7.8.4

git init submod
cd submod
echo "This is a submodule" > README.txt
git add .
git commit -m "Initial commit"
cd ..
git init prog
cd prog
echo "This is a program" > README.txt
git add .
git commit -a -m "Initial commit"
git checkout -b topic1
git submodule add ../submod
git commit -m "Added submodule"

git checkout master
#warning: unable to rmdir submod: Directory not empty
#Switched to branch 'master'

git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       submod/
#nothing added to commit but untracked files present (use "git add" to track)

git clean -fd
#Removing submod/

git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       submod/
#nothing added to commit but untracked files present (use "git add" to track)

2 个答案:

答案 0 :(得分:39)

这不是一个错误,它是记录在案的行为。来自man git-clean

  

如果未跟踪的目录由不同的git存储库管理,则默认情况下不会将其删除。

submod目录是一个不同的git存储库;如果你想删除它,Use -f option twice if you really want to remove such a directory

git clean -f -f -d submod 删除submod。请参阅下面的步骤(几乎完全相同;不同的git version和硬编码的submodule路径,否则git会吐出假人。


步骤


$ git --version
git version 1.7.5.4 # Note, different git-version. 

制作两个存储库


git init submod
cd submod
echo "This is a submodule" > README.txt
git add .
git commit -m "Initial commit"
cd ..
git init prog
cd prog
echo "This is a program" > README.txt
git add .
git commit -a -m "Initial commit"

submod分支中添加git submodule topic1


git checkout -b topic1
git submodule add /Users/simont/sandbox/SOTESTING/Subdir-testing/submod
git commit -m "Added submodule"

现在是有趣的部分。


$ git checkout master
warning: unable to rmdir submod: Directory not empty
Switched to branch 'master'

git status
# On branch master
# Untracked files:
#   (use "git add ..." to include in what will be committed)
#
#       submod/
#nothing added to commit but untracked files present (use "git add" to track)

尝试git-clean,然后实际 git-clean


git clean -fd
#Removing submod/

git status
# On branch master
# Untracked files:
#   (use "git add ..." to include in what will be committed)
#
#       submod/
#nothing added to commit but untracked files present (use "git add" to track)

$ # As we can see, we haven't actually removed anything yet. 
$ ls
README.txt  submod

$ git clean -f -f -d submod
Removing submod/

$ ls
README.txt

$ git status
# On branch master
nothing to commit (working directory clean)

答案 1 :(得分:3)

要在签出其他分支后删除“剩余”子模块,可以运行以下命令。 此命令将递归清理主存储库和所有子模块。 警告:这也会删除所有未跟踪的文件。

git clean -xffd && git submodule foreach --recursive git clean -xffd

要查看哪些文件将被删除而不实际删除,请添加-n标志。

git clean -nxffd && git submodule foreach --recursive git clean -nxffd