Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 490
Michael Durrant Avatar asked Apr 09 '12 14:04

Michael Durrant


People also ask

Does git remove empty directory?

Git does not delete empty folder but it has no concept of folders therefore it does not track folders (git only tracks paths to your files, not folders, it treats file paths like how you'd treat URLs on a website).

Can you push empty directory in git?

You can't. See the Git 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.

How do I remove a folder from my git repository?

Browse to the directory in the repository and branch that you want to delete. In the top-right corner, click "…", and then Delete directory. Review the list of files. Depending on your permissions and the branch protection rules, choose to either commit the change directly or propose the change using a pull request.


1 Answers

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.

like image 80
Jake Greene Avatar answered Sep 20 '22 08:09

Jake Greene