git clean 在切换分支时不会删除添加到分支的子模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9314365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
git clean is not removing a submodule added to a branch when switching branches
提问by Adrian Cornish
How do I get rid of submodules when switching branches. I do not understand why git clean says it removed the submodule but does not. Is this a bug? Below are cut&paste steps to reproduce.
切换分支时如何摆脱子模块。我不明白为什么 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)
回答by simont
This isn't a bug, it's documented behaviour. From man git-clean
:
这不是错误,而是记录在案的行为。来自man git-clean
:
If an untracked directory is managed by a different git repository, it is not removed by default.
如果未跟踪的目录由不同的 git 存储库管理,则默认情况下不会删除它。
The submod
directory is a different git
repository; if you want to remove it, Use -f option twice if you really want to remove such a directory
.
该submod
目录是不同的git
存储库;如果你想删除它,Use -f option twice if you really want to remove such a directory
.
git clean -f -f -d submod
doesremove submod
. See my steps below (almost identical; different git version
and hard-coded submodule
path because otherwise git
spits the dummy).
git clean -f -f -d submod
确实删除submod
. 请参阅下面的步骤(几乎相同;不同的git version
硬编码submodule
路径,否则会git
吐出假人)。
Steps
脚步
$ git --version
git version 1.7.5.4 # Note, different git-version.
Make the two repositories
创建两个存储库
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"
Add submod
as a git submodule
in topic1
branch.
添加submod
为git submodule
intopic1
分支。
git checkout -b topic1
git submodule add /Users/simont/sandbox/SOTESTING/Subdir-testing/submod
git commit -m "Added submodule"
Now for the interesting section.
现在是有趣的部分。
$ 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)
Attempt to git-clean
, then actuallygit-clean
.
企图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)
回答by jorisv92
To remove the "leftover" submodules after checking out a different branch, you can run the following. This command will recursively clean the main repository and all submodules. Warning: this will remove all untracked files as well.
要在签出不同的分支后删除“剩余”子模块,您可以运行以下命令。此命令将递归清理主存储库和所有子模块。 警告:这也将删除所有未跟踪的文件。
git clean -xffd && git submodule foreach --recursive git clean -xffd
To see which files will be deleted without actually deleting yet, add the -n flag.
要查看哪些文件将被删除而不实际删除,请添加 -n 标志。
git clean -nxffd && git submodule foreach --recursive git clean -nxffd
Key to the command is the double-f passed to Git clean. Without that, Git will not remove folders that are a submodule (i.e. folders containing a .git subfolder).
命令的关键是传递给 Git clean 的 double-f。否则,Git 将不会删除属于子模块的文件夹(即包含 .git 子文件夹的文件夹)。