.gitkeep
isn’t documented, because it’s not a feature of Git.
Git cannot add a completely empty directory. People who want to track empty directories in Git have created the convention of putting files called .gitkeep
in these directories. The file could be called anything; Git assigns no special significance to this name.
There is a competing convention of adding a .gitignore
file to the empty directories to get them tracked, but some people see this as confusing since the goal is to keep the empty directories, not ignore them; .gitignore
is also used to list files that should be ignored by Git when looking for untracked files.
.gitkeep
is just a placeholder. A dummy file, so Git will not forget about the directory, since Git tracks only files.
If you want an empty directory and make sure it stays 'clean' for Git, create a .gitignore
containing the following lines within:
# .gitignore sample
# Ignore all files in this dir...
*
# ... except for this one.
!.gitignore
If you desire to have only one type of files being visible to Git, here is an example how to filter everything out, except .gitignore
and all .txt
files:
# .gitignore to keep just .txt files
# Filter everything...
*
# ... except the .gitignore...
!.gitignore
# ... and all text files.
!*.txt
!
in front of .gitignore
? Is that in order to escape the dot ?
!
negates the following part, like it usually does in programming.
!.gitignore
in a git ignore file, either add the file then edit it, or force add it with appropriate contents ("*" to ignore everything, or nothing to simply make sure the folder exists) further example.
Since the git ignore file is already in the repo it is not necessary to not-ignore it - it is already tracked.
------ If it is not, and you do not do a forceful add, you might forget about it. In trivial cases, no problem, but if it is a bigger file you might be upset. Using !.gitignore
prevents you from shooting yourself in your foot. I prefer it, having burned myself in the past.
.gitignore
is a text file comprising a list of files in your directory that git will ignore or not add/update in the repository.
.gitkeep
Since Git removes or doesn't add empty directories to a repository, .gitkeep is sort of a hack (I don't think it's officially named as a part of Git) to keep empty directories in the repository.
Just do a touch /path/to/emptydirectory/.gitkeep
to add the file, and Git will now be able to maintain this directory in the repository.
.gitignore
s as you want, if you do not want to specify the full path to every folder every time.
.gitkeep
file but it will not track empty directories, Only folder track where .gitkeep
file exist. why so ?
.gitignore
s save you from specifying the full path to every folder every time? I think I'm missing something obvious.
Many people prefer to use just .keep
since the convention has nothing to do with git.
This is not an answer to the original question "What are the differences between .gitignore and .gitkeep?" but posting here to help people to keep track of empty dir in a simple fashion. To track empty directory and knowling that .gitkeep
is not official part of git,
https://i.stack.imgur.com/Smlxv.png
just add a empty (with no content) .gitignore
file in it.
So for e.g. if you have /project/content/posts
and sometimes posts
directory might be empty then create empty file /project/content/posts/.gitignore
with no content to track that directory and its future files in git.
Success story sharing
README
file in the otherwise empty subdirectory that contains a bit of information about what that subdirectory is going to be used for? It seems confusing to have a file called.gitkeep
that is not actually part of git..gitignore
file with two lines:*
and!.gitignore
is more than enough clarity to convey what is going on. If more elaboration is needed, add a comment to the top of the file using the#
syntax..keep
files instead of.gitkeep
to preserve these empty folders, since git is not the only source control system that does not track empty folders. More details here: github.com/rails/rails/issues/2800