Git checkout/pull 不会删除目录?
我有我的仓库@github。我在家做了一些工作并把它推到了github上。它涉及一些文件和目录的删除。现在我在我的工作箱上,在删除文件和目录之前,它有一份代码副本。
我发出以下命令:
git remote update
git checkout HEAD
git pull origin HEAD
它删除了应有的所有文件,但没有删除文件所在的目录。
两个问题:
- 为什么它不删除目录?
- 我可以在当前状态下发出 git 命令来删除它们吗?
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Git 不跟踪目录,因此它不会删除因合并或其他更改而变空的目录。但是,您可以使用 git clean -fd 删除未跟踪的目录(
-fd
标志意味着强制删除未跟踪的文件和目录)。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).我遇到了同样的问题,就我的构建服务(CI)而言。由于 GIT 会在不清理文件夹的情况下提取所有文件,因此之前由 CI 构建的所有 bin / obj 都是脏的,所以如果我删除测试项目,则 bin仍将包含 DLL 并会提及不存在的测试。
为了解决这个问题;这个命令似乎可以解决问题(至少对我来说),
其中 X 将删除所有未跟踪的文件:
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.
In order to solve this issue; this command seems to do the trick (at least for me)
where X will remove all untracked files:
作为大多数改变工作树的操作(拉取、合并、签出等)的一部分,git 将删除该操作清空的所有目录(即 git 删除最后一个文件)。
git 不会删除任何不完全为空的目录,因此,如果您隐藏或忽略了文件,那么仅仅因为 git 从该目录中删除最后一个跟踪文件并不一定意味着 git 将能够删除该目录。 git 不认为这是一个错误情况,所以不会抱怨它。
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 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 tracked file 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 会为它们创建所有目录(酷!),但是,如果路径中包含的所有文件都被移动或删除,它不会删除它们(不酷 ☹ 。 ..但有原因)。
解决方案(一旦拉取/快进/合并):
如果您在
clean
之前不stash
,您将丢失所有未跟踪的内容文件(不可逆)。注意:由于这也会清除所有被忽略的文件,因此您可能需要再次运行一些构建脚本来重新创建项目元数据(例如:
./gradlew eclipse
)。这还会删除空的目录,并且这些目录从来不是 git 文件路径的一部分。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 not delete them if all the files contained in a path are moved or deleted (not cool ☹ ... but there's reasons).
Solution (once you have pulled / fast-forwarded / merged):
If you don't
stash
beforeclean
, you will loose all your untracked files (irreversibly).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.对我来说,该目录是一个子模块,因此我需要运行
-f
两次:For me, the directory was a submodule, so I needed to run
-f
twice:Git 目前不跟踪目录(请参阅 git wiki),
是的,你既不能添加空目录,也不能 git 删除最终为空的目录。(编辑:谢谢,Manni,我错了!你不能添加 空目录,但是 git 会删除因为跟踪的内容被删除而变空的目录。)至于删除空目录的命令:这取决于你的操作系统。
对于 Linux,您可以使用,例如,
但是,这将删除所有空目录!
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 add empty directories, but git will remove directories that become empty because their tracked content was deleted.)As to the command to remove the empty directories: that depends on your operating system.
For Linux you could use, e.g.,
However, this will remove all empty directories!