What are the differences between .gitignore
and .gitkeep
? Are they the same thing with a different name, or do they both serve a different function?
I don't seem to be able to find much documentation on .gitkeep
.
What are the differences between .gitignore
and .gitkeep
? Are they the same thing with a different name, or do they both serve a different function?
I don't seem to be able to find much documentation on .gitkeep
.
.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.
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. –
Syllabize .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. –
Vernation README
file is the best solution to make git to track empty folders. –
Lindbergh .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 –
Spheno .empty
. I guess that makes more sense, at least for me. –
Fanti .gitignore
is confusing. Quite the opposite, it means you want to keep the folder AND ignore its contents. .gitkeep
doesn't prevent versioning disposable files by accident. –
Semination .gitkeep
makes more sense than .gitignore
, since you're not ignoring anything, you're just keeping the folder there. However, if the folder is never going to have tracked files in it, a .gitignore
ignoring those files makes the most sense. –
Privett .keep
is also a more general empty file, non-specific to git. I use this and see it around. –
Dominant .gitkeep
are simple and efficient way to track empty directories –
Graphitize .files
should be preferred to extraneous README files. –
Heptahedron .gitkeep
files. You can find it here: pypi.org/project/gitkeep2. You can simply install it by running pip3 install gitkeep2
and then use it by running gitkeep path/to/foo
. It supports recursivenes and adding messages to the .gitkeep
files in order to make them self-documented. –
Sair .gitkeep
file with a short explanation in it. This makes a hidden file, prevents confusion, does not break builds, contains documentation, and is not redundant. –
Forage .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 ? –
Sartorius !
negates the following part, like it usually does in programming. –
Conduit !.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. –
Paramagnet 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. –
Conduit (..)A dummy file, so git will not forget about the directory, since git tracks only files.(..)
well.. in Linux world everything is a file. And let don't forget that git was created by guy who create Linux in the first place. –
Genotype .gitkeep
file store text explaining why this directory is important to keep in source control. I've written a tiny utility that makes creating self-documented .gitkeep
files very easily —e.g. gitkeep path/to/foo -m "This is where we'll later add X stuff."
you can install it with pip3 install gitkeep2
and find it at pypi.org/project/gitkeep2 –
Sair .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. –
Conduit .gitkeep
file but it will not track empty directories, Only folder track where .gitkeep
file exist. why so ? –
Chlor .gitignore
s save you from specifying the full path to every folder every time? I think I'm missing something obvious. –
Shrewsbury Many people prefer to use just .keep
since the convention has nothing to do with git.
A file .gitignore
is used to tell Git which files and folders it should ignore. Often, these files are build artefacts, temporary files, or other types of files that don't belong in the repository. Git will disregard any directory or file that fits a pattern in the .gitignore
file.
Example:
# Ignore .DS_Store files
.DS_Store
# Ignore build artifacts
build/
# Ignore log files
*.log
Contrarily, a .gitkeep
file is utilised in Git to maintain a directory that would otherwise be empty. Git does not by default track empty folders, thus you must add a .gitkeep
file to that directory if you wish to keep it in your repository. The filename is more significant than the file's actual contents.
Example of .gitkeep
file
# This file is used to keep the directory empty in Git
In summary. The .gitignore
command is used to tell Git which files and folders to ignore. To maintain a Git directory that would otherwise be empty, use .gitkeep
. They are distinct from one another and have different functions.
.gitkeep
is not a git feature. This just became common practice. You can call the file whatever you want, because as long as there is at least on file in a folder, the folders will be tracked too. –
Mayor 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,
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.
.gitkeep
I found all content from community and none from official Git docs. I prefer to follow official docs first –
Satin If you want to ignore everything in a subdirectory but have the subdirectory itself (and for that the .gitkeep
file) added to Git, you have to add this to .gitignore
:
build/*
!build/.gitkeep
This means ignore everything under the build
directory, but exclude .gitkeep
from the ignore pattern.
© 2022 - 2024 — McMap. All rights reserved.