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

65,929

Solution 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.

Solution 2

git add --all
git clean -f -d
git commit -m "trying to remove folders"
git push
Share:
65,929
Michael Durrant
Author by

Michael Durrant

rails ruby rspec rock

Updated on July 11, 2020

Comments

  • Michael Durrant
    Michael Durrant almost 4 years

    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?

  • igniteflow
    igniteflow over 9 years
    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
  • Daniel Sokolowski
    Daniel Sokolowski almost 9 years
    Mercurial has a saner approach here - the directory is remove if empty: stackoverflow.com/a/3917322/913223 and superuser.com/questions/81204/…
  • Andry
    Andry almost 7 years
    This answer is incomplete. The git svn fetch does maintain empty folders. Even git svn rebase recreate empty folders if simply remove one.
  • alpha_989
    alpha_989 about 6 years
    @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?
  • joel
    joel over 5 years
    creates unnecessary commits
  • Aaron Franke
    Aaron Franke over 4 years
    Or just use git clean -fd
  • Benjamin John
    Benjamin John about 4 years
    Is there a way of achieving this without the need to commit and push changes?
  • Izhar Aazmi
    Izhar Aazmi about 3 years
    You do not need to commit. The last 2 commands are not required
  • StaNov
    StaNov over 2 years
    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.
  • user2987828
    user2987828 over 2 years
    git clean -i -d would be nicer.