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

人走茶凉 提交于 2019-11-28 16:56:20

问题


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.

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

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

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

I can directly rm the 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?


回答1:


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.




回答2:


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



回答3:


for deleting Empty folders

i wanted to delete an empty directory(folder) i created git can't delete it, after some research i learned Git doesn't track empty directories. If you have an empty directory in your working project you should simply removed it with

rm -r folderName

There is no need to involve Git. note this answer tested only on local machine repos,




回答4:


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.




回答5:


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"


来源:https://stackoverflow.com/questions/10074582/git-how-to-remove-empty-folder-and-push-that-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!