git - how to remove empty folder and push that change?
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
Comments
-
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 over 9 yearsThis 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 almost 9 yearsMercurial has a saner approach here - the directory is remove if empty: stackoverflow.com/a/3917322/913223 and superuser.com/questions/81204/…
-
Andry almost 7 yearsThis answer is incomplete. The
git svn fetch
does maintain empty folders. Evengit svn rebase
recreate empty folders if simply remove one. -
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 over 5 yearscreates unnecessary commits
-
Aaron Franke over 4 yearsOr just use
git clean -fd
-
Benjamin John about 4 yearsIs there a way of achieving this without the need to commit and push changes?
-
Izhar Aazmi about 3 yearsYou do not need to commit. The last 2 commands are not required
-
StaNov over 2 yearsI 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 over 2 years
git clean -i -d
would be nicer.