105

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
  • 1
    what do you mean the question remains?
    – manojlds
    Commented Apr 9, 2012 at 14:53

4 Answers 4

159

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.

4
  • Mercurial has a saner approach here - the directory is remove if empty: stackoverflow.com/a/3917322/913223 and superuser.com/questions/81204/… Commented Aug 5, 2015 at 23:01
  • 1
    This answer is incomplete. The git svn fetch does maintain empty folders. Even git svn rebase recreate empty folders if simply remove one.
    – Andry
    Commented Jul 30, 2017 at 1:13
  • @DanielSokolowski, is that the same approach in git, atleast when cloning a repo. I found that when cloning a repo containing a empty directory, git doesn't create the empty directory. Is this not a big issue, because now I have to check that the directory exists for writing files?
    – alpha_989
    Commented Apr 7, 2018 at 22:34
  • In my case I needed to supply -x in addition to -fd because my "empty" directories had ignored .DS_Store files in them. "Normally, only files unknown to Git are removed, but if the -x option is specified, ignored files are also removed." Commented Aug 30, 2023 at 17:57
15
git add --all
git clean -f -d
git commit -m "trying to remove folders"
git push
3
  • Is there a way of achieving this without the need to commit and push changes? Commented Apr 20, 2020 at 13:50
  • You do not need to commit. The last 2 commands are not required Commented Jun 9, 2021 at 12:56
  • 2
    git clean -i -d would be nicer. Commented Dec 13, 2021 at 16:57
0

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.

3
  • 3
    Or just use git clean -fd Commented Jan 27, 2020 at 11:04
  • In my case, this worked, and the git clean -fd did not work. I am still figuring out why that is the case. Commented Sep 12, 2022 at 6:23
  • 1
    It turns out, I have .DS-Store files in my "empty" directories. However, these files are in the .gitignore, so I think they are not tracked and should be deleted. But I have to supply an etra flag -x. The manual of git-clean says ``` Normally, only files unknown to Git are removed, but if the -x option is specified, ignored files are also removed. This can, for example, be useful to remove all build products. ``` Commented Sep 12, 2022 at 7:03
-6

A hacky way around this is to create a dummy file in the directory (assuming folderNameToRemove), commit it, then remove it. Removing it will also remove the directory.

Eg., create blank file in the directory. Then,

git add folderNameToRemove/blank
git commit --message="fix: empty folder removal"
git rm -r folderNameToRemove
git commit --amend --no-edit --allow-empty
  • The --allow-empty is a required argument if you're considering the following 2 arguments. Git will track the commit as if you're committing nothing as the net result of the sequential git add and git rm commands.
  • The --amend argument in second commit is optional but it provides cleaner commit with link to the previous proper commit.
  • The --no-edit argument in the second commit is optional as well together with --amend, but it could reduce the 'complex' requirement to change the metadata of the previous commit.
3
  • 3
    This approach does in fact work, but on the second step you want to delete the folder containing the file rm -rf name and then commit the change, the folder will now be gone
    – igniteflow
    Commented Jan 20, 2015 at 13:01
  • 2
    creates unnecessary commits
    – joel
    Commented Nov 30, 2018 at 14:27
  • 1
    I think this is actually the correct answer. OP wants the directory to disappear for all others without them running commands on their machines other than pull.
    – StaNov
    Commented Oct 26, 2021 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.