git - 如何删除空文件夹并推送该更改?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10074582/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:24:51  来源:igfitidea点击:

git - how to remove empty folder and push that change?

gitdirectoryrmrmdir

提问by Michael Durrant

How can I remove an empty folder locally and also have that happen for other collaborators that share the remote via pull-push? I know that folders aren't 'tracked' in that sense by git but the question remains.

我怎样才能在本地删除一个空文件夹,并且对于通过拉推共享远程的其他协作者也会发生这种情况?我知道 git 没有在这个意义上“跟踪”文件夹,但问题仍然存在。

e.g. I moved a file to another folder and committed the change (of the move).

例如,我将一个文件移动到另一个文件夹并提交了(移动的)更改。

But I can't git rm namethe folder as I get "doesn't match" git rmdir namedoesn't exist.

但我不能git rm name文件夹,因为我得到“不匹配” git rmdir name不存在。

I can do a git clean -f folderbut how does that get pushed up?

我可以做一个,git clean -f folder但它是如何被推高的?

I can directly rmthe file but how do I get that directory removal done correctly and pushed to the repository and then out to others when they pull so that their existing folder gets deleted?

我可以直接rm删除文件,但是如何正确完成目录删除并将其推送到存储库,然后在其他人拉取时再将其发送给其他人,以便删除他们现有的文件夹?

回答by Jake Greene

The short answer: You can't push changes to directories (added, removed, etc.) because Git does not track directories on their own.

简短的回答:您不能将更改推送到目录(添加、删除等),因为 Git 不会自行跟踪目录

According to the FAQ:

根据常见问题解答

Currently the design of the git index (staging area) only permits files to be listed, and nobody competent enough to make the change to allow empty directories has cared enough about this situation to remedy it.

Directories are added automatically when adding files inside them. That is, directories never have to be added to the repository, and are not tracked on their own.

目前 git 索引(暂存区)的设计只允许列出文件,没有人有足够的能力进行更改以允许空目录,已经足够关心这种情况来纠正它。

在目录中添加文件时会自动添加目录。也就是说,目录永远不必添加到存储库中,并且不会自行跟踪。

So as far as Git is concerned, your empty directory doesn't exist anymore.

因此,就 Git 而言,您的空目录不再存在。

I have found that getting in the habit of using git clean -fdremoves the need for pushing the removal of directories. However, git cleancan remove items you may not want removed (including any new files you have not yet committed) so I tend to first use git clean -fdnto see what willbe removed if I use the command.

我发现养成使用习惯可以git clean -fd消除推动删除目录的需要。但是,git clean可以删除您可能不想删除的项目(包括您尚未提交的任何新文件),因此我倾向于首先使用该命令git clean -fdn查看删除的内容。

It looks like you may be forced to talk to your fellow developers in order to clean up that directory.

看起来您可能被迫与其他开发人员交谈以清理该目录。

回答by Ekhtiar

git add --all
git clean -f -d
git commit -m "trying to remove folders"
git push

回答by Rich

You can't push empty folders. But if you want to clean out empty folders in your cloned/local repo, commit your latest changes.. Then simply delete all the files apart from .git folder in your local copy. Then reset all changes again, which puts back all the files but leaves out the empty directories it doesn't track.

您不能推送空文件夹。但是,如果您想清除克隆/本地存储库中的空文件夹,请提交您的最新更改。然后只需删除本地副本中除 .git 文件夹之外的所有文件。然后再次重置所有更改,这会放回所有文件,但会忽略它不跟踪的空目录。

回答by Graeme Hunter

A slightly hacky way around this is to create a fake file in the directory, commit it, then remove it. Removing it will also remove the directory. So create name/fake.txt

一个稍微有点hacky的方法是在目录中创建一个假文件,提交它,然后删除它。删除它也会删除目录。所以创建 name/fake.txt

git add name/fake.txt
git commit -m "message"
git rm name/fake.txt
git commit -m "message2"