git clean在切换分支时不会删除添加到分支的子模块 [英] git clean is not removing a submodule added to a branch when switching branches
问题描述
切换分支时,我该如何摆脱子模块?我不明白为什么git clean说删除了submodule,但没有。这是一个错误?
git --version
git版本1.7.8.4
下面是切割和粘贴步骤以进行重现。
git init submod
cd submod
echoThis is a submodule> README.txt
git add。
git commit -mInitial commit
cd ..
git init prog
cd prog
echoThis is a program> README.txt
git add。
git commit -a -m初始提交
git checkout -b topic1
git子模块add ../submod
git commit -m添加的子模块
git checkout master
#warning:无法rmdir submod:目录不为空
#切换到分支'master'
git status
#在分支上master
#未记录的文件:
#(使用git add< file> ...来包含将要提交的内容)
#
#submod /
#没有添加提交但未跟踪的文件存在(使用git add跟踪)
git clean -fd
#删除子模块/
git status
#在分支主机上
#未被跟踪的文件:
#(使用git add< file> ...来包含将要提交的内容)
#
#submod /
#没有添加到提交但未跟踪的文件存在(使用git add跟踪)
这不是一个错误,它是记录的行为。从 man git-clean
:
如果未跟踪的目录由不同的git仓库,默认情况下它不会被删除。
submod
目录是一个不同的 git
存储库;如果你想删除它,如果你真的想删除这样一个目录
,请使用-f选项两次。
git clean -f -f -d submod $> remove
submod
。请参阅下面的步骤(几乎相同;不同于 git版本
和硬编码子模块
路径,因为否则 git
吐出假人)。
步骤
$ git --version
git version 1.7.5.4#注意,不同的git版本。
制作两个存储库
git init submod
cd submod
echoThis is a submodule> README.txt
git add。
git commit -mInitial commit
cd ..
git init prog
cd prog
echoThis is a program> README.txt
git add。
git commit -a -m初始提交
添加 submod
作为 git子模块
在 topic1
分支中。
git checkout -b topic1
git submodule add / Users / simont / sandbox / SOTESTING / Subdir-testing / submod
git commit -m添加子模块
现在查看有趣的部分。
$ git checkout master
警告:无法rmdir submod:目录不为空
切换到分支'master'
git status
#分支大师
#未追踪的文件:
#(使用git add ...来包含将要提交的内容)
#
#submod /
#没有添加到提交但未跟踪的文件存在(使用git add跟踪)
尝试 git-clean
,然后实际 git-clean
$ b $ $ p $
git clean -fd
#Removing submod /
git状态
#在分支大师
#未经记录的文件:
#(使用git add ...来包含将要提交的内容)
#
#submod /
#没有添加到提交但未跟踪的文件存在(使用git add跟踪)
$#我们可以看到,我们还没有实际移除任何东西。
$ ls
README.txt submod
$ git clean -f -f -d submod
删除submod /
$ ls
README.txt
$ git status
#分支大师
没有提交(工作目录干净)
git clean -fd
#Removing submod /
git状态
#在分支大师
#未经记录的文件:
#(使用git add ...来包含将要提交的内容)
#
#submod /
#没有添加到提交但未跟踪的文件存在(使用git add跟踪)
$#我们可以看到,我们还没有实际移除任何东西。
$ ls
README.txt submod
$ git clean -f -f -d submod
删除submod /
$ ls
README.txt
$ git status
#分支大师
没有提交(工作目录干净)
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 --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)
This isn't a bug, it's documented behaviour. From man git-clean
:
If an untracked directory is managed by a different git repository, it is not removed by default.
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
.
git clean -f -f -d submod
does remove submod
. See my steps below (almost identical; different git version
and hard-coded submodule
path because otherwise git
spits the dummy).
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.
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 actually 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)
这篇关于git clean在切换分支时不会删除添加到分支的子模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!