I deleted all files from a directory in my work folder, but the directory itself still exists. After pushing the changes to a remote repository (GitHub) I checked my project and noticed that the directory was gone. Is it possible that Git deletes empty folders?
Does Git delete empty folders
gitgithub
Related Solutions
I'll start by addressing the easier case: del
in cmd will never do what you want, because it only removes files. Even then, without /s
it won't recurse and will only remove files in the top-level directory.
So, for cmd, you should be using rmdir /s
to recursively delete all files and folders. Add /q
if you want to disable the confirmation prompt.
Now, PowerShell. Remove-Item
(which is what rm
and del
alias to) with just -Recurse
will not recurse into hidden directories, and will therefore not delete their contents. Which leads to your error - which is not a "permission denied" but rather a "directory not empty".
To get around this, you can pass the -Force
parameter (equivalent to *nix -f
). So rm -r -force
or Remove-Item -Recurse -Force
should work.
I'm not entirely sure how you're getting a non-hidden .git
directory - GfW (admittedly an older 2.9.2) creates them as hidden for me. In cmd, run attrib .git
to check if it's hidden. Once un-hidden, a Remove-Item -Recurse
works without -Force
.
As hinted in GitHub help:
Create a new repository on GitHub.
Open Git Bash.
Change the current working directory to your local project.
Initialize the local directory as a Git repository.
$ git init
Add the files in your new local repository. This stages them for the first commit.
$ git add .
Commit the files that you've staged in your local repository.
$ git commit -m "First commit"
At the top of your GitHub repository's Quick Setup page, click to copy the remote repository URL.
In the Command prompt, add the URL for the remote repository where your local repository will be pushed.
$ git remote add origin <remote repository URL> # Sets the new remote $ git remote -v # Verifies the new remote URL
Push the changes in your local repository to GitHub if there is a remote branch called
master
(ormain
if that's what you're using)$ git push origin master
Otherwise you will have to name local branch first by
$ git branch -m <new_name>
and then push it to add a new branch called <new_name>
$ git push origin -u <new_name>
If you still end up with errors like "Updates were rejected because the remote contains work that you do not have locally", this is normally because that the remote repo is recently created manually. Make sure you are not overwriting anything on the remote end before you force push local git folder to it using
$ git push origin -u -f <new_name>
Best Answer
Why is the directory not shown?
Git does not track directories; it only tracks files.
If there are no files in a directory, that directory does not “exist” to Git when adding or removing files. Particularly, a directory will disappear from Git's index when you've deleted all files from it and add that change to the index. Vice-versa, a directory will not be added via
git add
if it's empty.In other words: If you can see the directory locally in your file browser, but it disappeared from GitHub, you most likely removed all files from the directory, added that change to the index, and committed and pushed it.
How do I track an empty directory, then?
If you want to explicitly track an empty directory, you have to create a file in it. Since Git won't track empty directories, you have to trick it into doing so by adding a file in the directory to Git's index.
Usually, people store a file called
.gitkeep
in a directory that they wish to track, but where the directory should stay empty for the time being. You can give the file any other name, of course, but the name.gitkeep
is a convention. The.gitkeep
file (due to starting with a.
) will not be shown by file listings on most systems.Instead of
.gitkeep
, some users also like to put aREADME
file there instead, ideally with a short description of why the directory has to exist in the first place.Example
Here, the
foo
directory only gets added to the index once a file is in it.What if I really want to track an empty directory?
That all said, in principle, the underlying data structure allows Git to store an empty directory, since it would be represented by an empty “tree”. Some further reading here and here.