Git | Handling Empty Folders
Does Git Handle Empty Folders?
The simple answer is no.
Two git use cases pertaining to empty folders occur from time to time. When you want to
- remove an empty folder from the working area.
- create, add, commit and push an empty folder.
Remove empty folders from Git
The 2 ways to handle removing an empty folder from a Git project is to
- prevent the situation occuring
- let the merge remove the empty folder
- let the passage of time handle it
1. Prevent Empty Folders Occuring
Git neither tracks nor recognises empty folders so preventing this impasse occurring is preferable to curing it after it occurs.
At the point when the directory has contents you should either move or remove the directory.
Prevent Empty Folders Occuring
This git command renames (moves) the folder and the empty scenario will not arise.
git mv non-empty-folder another-folder
This git command deletes (removes) the folder before the empty scenario arises.
git rm -r non-empty-folder
Basically do not do a
git rm non-empty-folder/unwanted-file.txt if the unwanted-file.txt (or folder) is the last item. Stop yourself falling into the empty folder hole – by removing the folder one level up.
2. Let the merge remove the empty folder
Merging the branch that you are working on – back into master – deletes the empty folder.
The empty folder isn’t recognized by Git so even though it refuses to let you commit a deleted empty folder – it faithfully deletes the folder when your branch is merged back into the master branch.
3. Git Empty Folders | Passage of Time
Time is a great healer. Just delete the empty folder in your working directory. Over time your fellow team mates working on the same branch will refresh with a
git checkout and the empty folder disappears.
Create Add Commit Push Empty Folders
What if you have software that uses the folder as some sort of flag? You should probably change the software.
If for some reason you can’t change the software then add a dummy file into the “flag” directory and Git will oblige by letting you add, commit and push the folder into your repository.