Keep Some Files in an Ignored Folder with .gitignore in Git
To track only some files in a folder that .gitignore ignores, write the folder as logs/* instead of logs/, then add a line starting with ! below it, such as !logs/.gitkeep. If you write logs/, Git ignores the whole folder, and a ! line written later has no effect. Use this pattern when you want to keep an empty log or cache folder (with a .gitkeep file), or when you want to commit only the explanation files (such as a README) in a data folder.
I tested on Ubuntu 24.04 with Git 2.43.0. I set the author name and email to example values (Neko <neko@example.com>), and set the commit dates with the environment variables GIT_AUTHOR_DATE and GIT_COMMITTER_DATE. I did not test on Windows or macOS, or the patterns in .git/info/exclude or in the global exclude setting (core.excludesFile).
- 1. Example repository: keep only logs/.gitkeep
- 2. Writing logs/ and then adding "!" does not bring files back
- 3. Writing logs/* makes only .gitkeep tracked again
- 4. The "!" line must come after the ignore line
- 5. For a file deep in the tree, re-include the folders on the way too
- 6. You can also use git add -f without changing .gitignore
Example repository: keep only logs/.gitkeep
I start with a logs/ folder that contains .gitkeep (an empty file), plus app.log and error.log, which the app writes. I want to track only .gitkeep.
$ ls -A logs
.gitkeep
app.log
error.log
$ git status --short
?? logs/
git status shows an untracked folder as one entry, like logs/. With -uall, it lists each file inside the folder separately (see the description of -u in the git status reference). In this article I use -uall so I can see which files are ignored.
$ git status --short -uall
?? logs/.gitkeep
?? logs/app.log
?? logs/error.log
Writing logs/ and then adding "!" does not bring files back
First I ignore the folder with logs/, then I add !logs/.gitkeep on the next line.
$ cat .gitignore
logs/
!logs/.gitkeep
$ git status --short -uall
?? .gitignore
logs/.gitkeep does not appear in the list. git check-ignore -v shows which line ignored a file. Its output is the file name, the line number, the pattern, and then the path.
$ git check-ignore -v logs/.gitkeep
.gitignore:1:logs/ logs/.gitkeep
Line 1, logs/, still ignores the file. Line 2, !logs/.gitkeep, is never used. If I try git add on the file, Git says that the logs folder is ignored.
$ git add logs/.gitkeep
The following paths are ignored by one of your .gitignore files:
logs
hint: Use -f if you really want to add them.
hint: Turn this message off by running
hint: "git config advice.addIgnoredFile false"
The gitignore reference says that you cannot use ! to re-include a file when its parent folder is excluded. For speed, Git does not look inside an excluded folder. So a line that targets a file inside that folder has no effect, wherever you write it.
Writing logs/* makes only .gitkeep tracked again
I change line 1 to logs/*. This pattern matches the items inside the logs folder, not the folder itself, so the folder is not ignored. Git looks inside the folder and decides for each file separately.
$ cat .gitignore
logs/*
!logs/.gitkeep
$ git status --short -uall
?? .gitignore
?? logs/.gitkeep
git check-ignore -v also shows the ! line for files that match it and are therefore not ignored. .gitkeep matches the ! line on line 2, and the two log files match line 1. The -n option makes check-ignore also list paths that match no pattern.
$ git check-ignore -v -n logs/.gitkeep logs/app.log logs/error.log
.gitignore:2:!logs/.gitkeep logs/.gitkeep
.gitignore:1:logs/* logs/app.log
.gitignore:1:logs/* logs/error.log
git add -n . (which shows what would be added without adding it) also confirms that only .gitkeep is included.
$ git add -n .
add '.gitignore'
add 'logs/.gitkeep'

When I commit now, only .gitkeep from the logs folder is included.
$ git add . && git commit -q -m "logs フォルダーを .gitkeep だけ管理する"
$ git ls-files
.gitignore
app.js
logs/.gitkeep
$ git log --stat -1
commit 7495bd05aae5ac549f112523d698a950e6fe8ab5
Author: Neko <neko@example.com>
Date: Fri Nov 13 10:00:00 2026 +0900
logs フォルダーを .gitkeep だけ管理する
.gitignore | 2 ++
logs/.gitkeep | 0
2 files changed, 2 insertions(+)
After the commit, when I append to app.log, git status shows nothing.
$ echo "next" >> logs/app.log && git status --short -uall
The "!" line must come after the ignore line
In the same .gitignore, when several lines match, the last matching line decides the result (see the precedence explanation in the gitignore reference). If I reverse the order and put !logs/.gitkeep first, the later logs/* line ignores .gitkeep again.
$ cat .gitignore
!logs/.gitkeep
logs/*
$ git status --short -uall
?? .gitignore
$ git check-ignore -v logs/.gitkeep
.gitignore:2:logs/* logs/.gitkeep
For a file deep in the tree, re-include the folders on the way too
Next, I want to track only data/samples/readme.md. The data/ folder also contains raw/big.csv and samples/small.csv. With just the two lines data/* and !data/samples/readme.md, readme.md is not brought back.
$ cat .gitignore
logs/*
!logs/.gitkeep
data/*
!data/samples/readme.md
$ git status --short -uall
M .gitignore
$ git check-ignore -v data/samples/readme.md
.gitignore:3:data/* data/samples/readme.md
data/* also matches the samples folder inside data. Because the parent samples folder is excluded, readme.md cannot be re-included with !, just as with logs/.
So I re-include the intermediate folder with !, ignore its contents again with /*, and then re-include the file I want to keep.
$ cat .gitignore
logs/*
!logs/.gitkeep
data/*
!data/samples/
data/samples/*
!data/samples/readme.md
$ git status --short -uall
M .gitignore
?? data/samples/readme.md
$ git check-ignore -v -n data/raw/big.csv data/samples/small.csv data/samples/readme.md
.gitignore:3:data/* data/raw/big.csv
.gitignore:5:data/samples/* data/samples/small.csv
.gitignore:6:!data/samples/readme.md data/samples/readme.md
Each of the three files is decided by a different line. The official reference also has an example of the same shape: four lines, /*, !/foo, /foo/*, and !/foo/bar, exclude everything except foo/bar. In both cases, the intermediate folder gets one line that re-includes it and one line that ignores its contents.
You can also use git add -f without changing .gitignore
You can keep .gitignore as logs/ and add a specific file with git add -f. The -f option lets you add files that are ignored (see the git add reference).
$ cat .gitignore
logs/
$ git add logs/manual.txt
The following paths are ignored by one of your .gitignore files:
logs
hint: Use -f if you really want to add them.
hint: Turn this message off by running
hint: "git config advice.addIgnoredFile false"
$ git add -f logs/manual.txt && git status --short
A logs/manual.txt
After I commit it and start tracking the file, changes to it show up in git status, even though it still matches .gitignore.
$ git commit -q -m "logs/manual.txt を追加" && git status --short
$ echo "edited" >> logs/manual.txt && git status --short
M logs/manual.txt
In this case git check-ignore -v logs/manual.txt prints nothing and exits with code 1. The file is tracked, so Git does not treat it as ignored. To see which .gitignore line matches a tracked file, add --no-index so that check-ignore ignores the index (the record of tracked files) (see the git check-ignore reference).
$ git check-ignore -v --no-index logs/manual.txt
.gitignore:1:logs/ logs/manual.txt
Even if you add files with git add -f, .gitignore still has only the one line logs/. If you want the exceptions written down in .gitignore, the logs/* and ! pattern is the better choice.




