Git checkout/pull 不删除目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1499157/
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 checkout/pull doesn't remove directories?
提问by mculp
I've got my repo @ github. I did some work at home and pushed it to github. It involved some deleting of files and directories. Now I'm on my work box, which had a copy of the code before deleting the files and directories.
我有我的回购@github。我在家里做了一些工作,并把它推到了github。它涉及一些文件和目录的删除。现在我在我的工作箱上,它在删除文件和目录之前有一份代码副本。
I issued the following:
我发布了以下内容:
git remote update
git checkout HEAD
git pull origin HEAD
It deleted all of the files it should have, but not the directories the files were in.
它删除了它应该拥有的所有文件,但没有删除文件所在的目录。
Two questions:
两个问题:
- Why did it not remove the directories?
- Is there a git command I can issue in the current state to remove them?
- 为什么不删除目录?
- 我可以在当前状态下发出 git 命令来删除它们吗?
回答by mipadi
Git doesn't track directories, so it won't remove ones that become empty as a result of a merge or other change. However, you can use git clean -fd
to remove untracked directories (the -fd
flag means force removal of untracked files and directories).
Git 不跟踪目录,因此它不会删除由于合并或其他更改而变空的目录。但是,您可以使用git clean -fd
删除未跟踪目录(该-fd
标记的装置˚F奥尔塞去除未跟踪文件和directories)。
回答by CB Bailey
As part of most operations that alter the working tree (pull, merge, checkout, etc.) git will remove any directories which are made empty by that operation (i.e. git removed the last file).
作为大多数改变工作树的操作(拉、合并、检出等)的一部分,git 将删除任何由该操作变为空的目录(即 git 删除了最后一个文件)。
git won't remove any directories that aren't completely empty, so if you have hidden or ignored files then just because git removes the last trackedfile from that directory doesn't necessarily mean that git will be able to remove that directory. git doesn't consider this to be an error condition so won't complain about it.
git 不会删除任何不完全为空的目录,因此如果您有隐藏或忽略的文件,那么仅仅因为 git从该目录中删除了最后一个跟踪的文件并不一定意味着 git 将能够删除该目录。git 不认为这是一个错误条件,所以不会抱怨它。
回答by qwertzguy
Git doesn't track directories, files (with their path). Git creates all the directories for those paths if they don't exist yet (cool!), however it does notdelete them if all the files contained in a path are moved or deleted (not cool ? ... but there's reasons).
Git 不跟踪目录、文件(及其路径)。Git会创建这些路径中的所有目录,如果它们不存在,但(爽!),但它并没有删除它们如果包含路径中的所有文件被移动或删除(不酷?...但有原因)。
Solution(once you have pulled / fast-forwarded / merged):
解决方案(一旦你拉/快转/合并):
git stash --include-untracked
git clean -fd
git stash pop
If you don't stash
before clean
, you will loose all your untracked files (irreversibly).
如果您stash
之前不这样做clean
,您将丢失所有未跟踪的文件(不可逆转)。
Note: since this cleans all ignored files too, you might need to run some of your build scripts again to recreate project metadata (ex: ./gradlew eclipse
). This also delete directories that are empty and that were never part of paths of git files.
注意:由于这也会清除所有被忽略的文件,您可能需要再次运行一些构建脚本来重新创建项目元数据(例如:)./gradlew eclipse
。这也会删除空的目录,这些目录从来不是 git 文件路径的一部分。
回答by Yogurtu
I had same issue, in my case on build service (CI).. as GIT pulls all files without cleaning folders, all the bin / obj that were previously builded by the CI are dirty, so If I remove a test project, the bin will still contain the DLL and will mention tests that does not exist.
我在构建服务 (CI) 的情况下遇到了同样的问题。由于 GIT 在不清理文件夹的情况下提取所有文件,CI 之前构建的所有 bin / obj 都是脏的,所以如果我删除一个测试项目,bin仍将包含 DLL 并会提及不存在的测试。
In order to solve this issue; this command seems to do the trick (at least for me)
为了解决这个问题;这个命令似乎可以解决问题(至少对我而言)
git clean -fd -x
where X will remove all untracked files:
其中 X 将删除所有未跟踪的文件:
-X Remove only files ignored by Git. This may be useful to rebuild everything from scratch, but keep manually created files.
-X 仅删除 Git 忽略的文件。这对于从头开始重建所有内容可能很有用,但保留手动创建的文件。
回答by janko
Git does not track directories currently (see git wiki), that is, you neither can add empty directories nor will git remove directories that end up empty.(EDIT:Thanks, Manni, I was wrong! You can't addempty directories, but git will removedirectories that become empty because their tracked content was deleted.)
Git 当前不跟踪目录(请参阅git wiki),也就是说,您既不能添加空目录,也不会 git 删除最终为空的目录。(编辑:谢谢,曼尼,我错了!你不能添加空目录,但 git 会删除因为它们的跟踪内容被删除而变空的目录。)
As to the command to remove the empty directories: that depends on your operating system.
至于删除空目录的命令:这取决于您的操作系统。
For Linux you could use, e.g.,
对于 Linux,您可以使用,例如,
find -depth -type d -empty -exec rmdir {} \;
However, this will remove allempty directories!
但是,这将删除所有空目录!