git clean排除嵌套子目录 [英] Git clean exclude nested sub directory
问题描述
我在使用git clean时遇到麻烦,并排除了嵌套dirs的选项.
I'am having trouble with git clean and exclude options for nested dirs.
我想从回购中清除所有未提交的文件,但vendor/bundle
目录除外.
我的测试仓库很喜欢:
I would like to clean all uncommitted files from repo excluding vendor/bundle
dir.
My test repo loks like:
debugg-dir/
.git/
file.txt
not-commited-file
not-commited-folder
another-not-commited-file
vendor/
bundle/
another-not-commited-file
使用以下命令重现测试仓库:
Reproduce test repo with:
git init debugg-dir && cd debugg-dir
touch file.txt && g a . && git ci -m "Commit"
mkdir -p not-commited-folder && touch not-commited-folder/another-not-commited-file
mkdir -p vendor/bundle && touch vendor/bundle/another-not-commited-file && touch not-commited-file
Git clean命令:
Git clean command:
git clean -d -x -n -e vendor/bundle
清洁后应该具有:
debugg-dir/
.git/
file.txt
vendor/
bundle/
another-not-commited-file
有什么适当的方法可以从git clean命令中排除嵌套目录吗?
Is there any proper way to exclude nested dir from git clean command?
#
说明:
对于这种情况,没有干净"的解决方案.
Git clean排除了git clean -d -x -n -e dir_name
的dirs,但这不适用于嵌套的dirs.
是git中的这个错误,还是有充分的理由呢?有关为什么此方法无效的更多信息,您可以在源代码中找到 .长话短说,排除模式仅适用于模式中第一个'/'之前的字符串.
There is no "clean" solution for this situation.
Git clean excludes dirs with git clean -d -x -n -e dir_name
but this doesn't work with nested dirs.
Is this bug in git or there is some good reason for that? More info why this doesnt work you can find in source. Long story short, exclude pattern only works for strings till first '/' in pattern.
我的解决方案:
cd vendor && git clean -dxf -e bundle && cd ..
git clean -dxf -e vendor
有了这个,我设法只保留了嵌套的目录及其内容.
With this i managed to only keep nested dir and it's contents.
推荐答案
按照git clean --help
git-clean-从工作树中删除未跟踪的文件
git-clean - Remove untracked files from the working tree
如果您添加弗洛伊德·平克(Floyd Pink)关于-d
的解释(不久,该选项还允许删除未跟踪的目录,而不仅仅是文件),这就是为什么您也删除了vendor
的原因.
现在,假设您只想删除 我认为您应该not-commited-file
(因此,任何未跟踪的目录和another-not-commited-file
都不要)git clean
交互模式, so
If you add to this Floyd Pink's explanation about -d
(shortly, the option allows to remove also untracked directories and not just files), then that is why you get vendor
removed as well.
Now, supposedly you want to remove only I think you should not-commited-file
(so, neither any untracked directory nor another-not-commited-file
)git clean
interactive mode, so
git clean -i
这将询问您对每个未跟踪的文件做什么(只有文件,如果您也想询问目录,请添加-d
.).
OP进行问题编辑后,进行编辑:您也要删除目录,因此运行
which will ask you what to do for each untracked file (only files, add -d
if you want to asked for directory too).
EDIT after OP's question editing: you want to remove directories too, so run
git clean -i -d
编辑2 :由于手册中对-e
的含义不清楚,因此我在Google上进行了搜索,找到了
EDIT 2: since the meaning of -e
was not clear to me from the manual, I googled it and found this. I suggest reading the conversation since it explains the real meaning of -e
which is not how the OP intended it (or can be understood from the manual)
编辑3 ,有关-e
开关的更多信息.遵循在 edit 2 中找到的链接,我决定尝试一下.这里的结果,希望对您有所帮助.-e
.
.gitignore
的内容,所以我不提交临时文件:
EDIT 3, more about -e
switch. Following the link I found in edit 2, I decided to try it out. Here the result, which I hope will help you to understand -e
.
Content of .gitignore
, so I don't commit temporary files:
*.tmp
我发出了命令:
echo "Temporary file" > sample.tmp
git st //which of course shows *nothing to commit, working directory clean*
git clean -fX -e \!sample.tmp
结果是删除了所有带有tmp扩展名的文件(由于-X
),但删除了sample.tmp
.因此,总的来说,根据我的理解,-e
的真正作用是,如果我错了,请纠正我,这不是从清洁过程中排除图案,而是
The result is that ALL files with tmp extension are deleted (due to -X
) BUT sample.tmp
. So, in conclusion, what -e
really does, in my understanding and please correct me if in the I am wrong, is NOT to exclude patterns from the cleaning process but
从清除规则中排除模式(在我的情况下,规则是删除所有.tmp文件,从该文件中我手动排除了sample.tmp).
to exclude patterns from the rule of cleaning (in my case the rule was to remove ALL .tmp files, from which I manually excluded sample.tmp).
这篇关于git clean排除嵌套子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!