How to Commit an Empty Folder in Git
In Git, a folder with nothing in it is not added by git add and cannot be committed. If you put one empty file inside it, the folder is committed together with that file, and the folder is also created in a clone. This is useful when you want the folder itself in the repository, but its contents can be empty, such as a place for logs, temporary files, or uploaded files.
The test environment was Ubuntu 24.04 and Git 2.43.0. I set the author name and email to example values (Neko <neko@example.com>). The clone source was a remote repository (a bare repository) created in the same test environment. I did not test how it appears when pushed to a service such as GitHub, and I did not test on Windows or macOS.
- 1. An empty folder does not show up in git status
- 2. git add succeeds, but nothing can be committed
- 3. An empty folder is treated as untracked
- 4. Commit an empty file named .gitkeep inside the folder
- 5. The folder is also created in a clone
- 6. Ignore the contents and keep only the folder
- 7. If you delete all files inside, the folder also disappears
An empty folder does not show up in git status
I started with a repository named neko-app that has only index.html committed. Here I created an empty folder logs for logs. The 0 from ls -A logs | wc -l shows that logs contains no files at all.
$ mkdir logs
$ ls -A
.git
index.html
logs
$ ls -A logs | wc -l
0
Running git status in this state does not show logs. Adding --untracked-files=all, which lists untracked files one by one, made no difference.
$ git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
$ git status --untracked-files=all
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
git add succeeds, but nothing can be committed
git add logs did not give an error, and the exit code was 0. However, git status --short printed 0 lines, so nothing was staged. Running git commit after that stopped with "nothing to commit", and the exit code was 1.
$ git add logs; echo "終了コード: $?"
終了コード: 0
$ git status --short | wc -l
0
$ git commit -m "ログ用のフォルダを作る"; echo "終了コード: $?"
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
終了コード: 1
echo "終了コード: $?" is there to show the exit code of the previous command. You do not normally need it.
Git records a folder only when there is a file inside it. git svn is a command for working with Subversion repositories. The description of --rmdir in the official Git 2.43.0 reference also states that Subversion can manage empty directories, but Git cannot version empty directories.
An empty folder is treated as untracked
Although git status does not show it, Git is not completely ignoring empty folders. git ls-files --others --directory, which lists untracked items, did show logs/. When --no-empty-directory is added, nothing is shown. The description of --no-empty-directory in the official Git 2.43.0 reference also states that this option does not list empty directories.
$ git ls-files --others --directory
logs/
$ git ls-files --others --directory --no-empty-directory | wc -l
0
$ git clean -n -d
Would remove logs/
$ git ls-tree -r --name-only HEAD
index.html
When git clean, which removes untracked files, is given -d to include folders, logs/ is included in the removal target. (I used -n, so it only displayed the targets and did not delete anything.) The last git ls-tree lists the files in the current commit. It shows only index.html, and logs is not included.
Commit an empty file named .gitkeep inside the folder
I created an empty file .gitkeep inside logs. Now logs/ appears in git status.
$ touch logs/.gitkeep
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
logs/
nothing added to commit but untracked files present (use "git add" to track)
With the same git add logs as before, logs/.gitkeep was staged and the commit succeeded.
$ git add logs
$ git status --short
A logs/.gitkeep
$ git commit -m "ログ用のフォルダを作る"
[main 0b42b79] ログ用のフォルダを作る
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 logs/.gitkeep
$ git ls-tree -r --name-only HEAD
index.html
logs/.gitkeep
What went into the commit was the file logs/.gitkeep, and logs is recorded as the location of that file. As the commit output shows (0 insertions(+) and create mode 100644), .gitkeep is committed as a regular file with 0 bytes, just like any other file.
The folder is also created in a clone
I pushed this commit, then cloned it into another location to check. I left neko-app and moved to the folder where neko-app and the remote neko-app.git sit side by side, then cloned. Inside neko-app-copy there is a logs folder, and .gitkeep is inside it.
$ git push -q origin main
$ cd ..
$ git clone -q neko-app.git neko-app-copy
$ ls -A neko-app-copy
.git
index.html
logs
$ ls -A neko-app-copy/logs
.gitkeep
Ignore the contents and keep only the folder
There are cases, such as a place for logs or temporary files, where you want the folder in the repository but do not want the files created inside it to be committed. In that case, instead of .gitkeep, put a .gitignore with the following two lines inside the folder. I went back to the original neko-app to test this.
$ cd neko-app
$ mkdir tmp
$ printf "*\n!.gitignore\n" > tmp/.gitignore
$ cat tmp/.gitignore
*
!.gitignore
The * on the first line ignores all files in tmp. Because this would also ignore .gitignore itself, the !.gitignore on the second line removes only that file from the ignore targets. The leading ! is a way to cancel an earlier ignore rule for a file so that Git handles it again (gitignore pattern format in the official Git 2.43.0 reference).
To test, I created cache.txt inside tmp and then checked the status. In git status --short, ?? means an untracked file, and with --ignored, !! means an ignored file. Only .gitignore was a candidate for commit, and cache.txt was ignored.
$ printf "一時ファイル\n" > tmp/cache.txt
$ git status --short --untracked-files=all
?? tmp/.gitignore
$ git status --short --ignored --untracked-files=all
?? tmp/.gitignore
!! tmp/cache.txt
$ git check-ignore -v tmp/cache.txt
tmp/.gitignore:1:* tmp/cache.txt
git check-ignore -v is a command that shows which file and which line's rule caused a path to be ignored. Here it shows that line 1 of tmp/.gitignore, the *, is what ignores the file.
git add tmp staged only tmp/.gitignore for the commit.
$ git add tmp
$ git status --short
A tmp/.gitignore
$ git commit -q -m "一時ファイル用のフォルダを作る"
$ git ls-tree -r --name-only HEAD
index.html
logs/.gitkeep
tmp/.gitignore
As described in the -x entry in the official git clean reference, adding -x stops the standard ignore rules from being used, so ignored files also become targets for removal. If the files in tmp are needed, check the list of files that would be deleted with -n before running the command with -x.
If you delete all files inside, the folder also disappears
A folder remains in the repository only while it contains files. When I deleted logs/.gitkeep with git rm, logs disappeared from the working tree, and it also disappeared from the list of files in the commit.
$ git rm logs/.gitkeep
rm 'logs/.gitkeep'
$ ls -A
.git
index.html
tmp
$ git commit -q -m "ログ用のフォルダをやめる"
$ git ls-tree -r --name-only HEAD
index.html
tmp/.gitignore
While you want to keep the folder empty, do not delete .gitkeep.





