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

我怎样才能在本地删除一个空文件夹,同时让其他通过 pull-push 共享远程的合作者也这样做呢?我知道在这个意义上,git 不会“跟踪”文件夹,但问题仍然存在。

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

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

我可以做 git clean -f folder但是怎么把它推上去呢?

我可以直接 rm的文件,但我如何得到正确的目录删除完成,并推到存储库,然后推出到其他人时,他们拉,以便他们现有的文件夹得到删除。

73280 次浏览

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

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.

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

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

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

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

git add name/fake.txt
git commit -m "message"
git rm name/fake.txt
git commit -m "message2"
git add --all
git clean -f -d
git commit -m "trying to remove folders"
git push

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.