2572

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.

7 Answers 7

4477

.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.

25
  • 548
    Wouldn't it be a better solution to put a 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.
    – tamouse
    Jul 14, 2012 at 21:45
  • 557
    @tamouse many times, the path of the empty directory (e.g. names of the folders) is sufficient to express it's purpose (examples: templates/cache, upload/thumbs etc). In these cases putting a readme into each of these feels redundant. Jan 31, 2013 at 8:33
  • 25
    people who want to keep track of empty directories should be indicating in a README that the directory has to be created or creating the directories using their build tool or whatever tool(s) require the directory to exist ;/
    – user9903
    Jul 22, 2013 at 17:06
  • 53
    @tamouse, @omouse: A .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.
    – yurisich
    Sep 2, 2013 at 16:32
  • 84
    It's worth to notice that the popular Rails framework has slightly changed this convention using .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 Nov 25, 2013 at 9:43
468

.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
7
  • 3
    I like this practice myself. If there was source code in these directories there would be no need for .gitkeep and general it is temp/cache/user content which during testing would be generated anyways causing you to have to also .gitignore those files
    – chrisan
    Aug 28, 2012 at 11:37
  • 1
    Why do you need ! in front of .gitignore ? Is that in order to escape the dot ?
    – Will
    Oct 5, 2012 at 21:37
  • 10
    @Will - No, the ! negates the following part, like it usually does in programming.
    – sjas
    Oct 8, 2012 at 15:02
  • 3
    There is no need to put !.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.
    – AD7six
    May 13, 2014 at 14:26
  • 22
    From your link: 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.
    – sjas
    May 13, 2014 at 14:44
150
.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.

5
  • 23
    You can have as many .gitignores as you want, if you do not want to specify the full path to every folder every time.
    – sjas
    Sep 17, 2012 at 17:54
  • 1
    I try to mention empty directories list into .gitkeep file but it will not track empty directories, Only folder track where .gitkeep file exist. why so ? Feb 14, 2018 at 6:46
  • 3
    .gitkeep does not work like .gitignore. It's not a list of directories to keep around. It is merely an empty file that lives in the directory you want to keep around. It can be named anything you want .keep, etc. So you can have a directory like /foo/bar and the gitkeep file will be /foo/bar/.gitkeep
    – Jim Munro
    Feb 19, 2018 at 4:48
  • 1
    @sjas how would having multiple .gitignores save you from specifying the full path to every folder every time? I think I'm missing something obvious.
    – Willwsharp
    Aug 14, 2019 at 14:56
  • @Willwsharp Assuming you have /abc/def/somefile. If your git is in /abc/, you would have to ignore ./dev/somefile. If you place your gitgnore inside /abc/def, then you only need to ignore ./somefile
    – Default
    Jul 11, 2020 at 15:19
9

Many people prefer to use just .keep since the convention has nothing to do with git.

2
  • 18
    arguably, the reason for the convention is almost explicitly because of git
    – JDPeckham
    Sep 5, 2021 at 17:52
  • 2
    @JDPeckham Perforce also needs files present to track a folder.
    – Polygnome
    Jun 7, 2022 at 12:13
4

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.

2

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,

enter image description here

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.

6
  • 11
    I disagree. The fact that .gitkeep is not an official part of git is absolutely not a reason to misuse .gitignore. An empty .gitignore is confusing and meaningless. Where as an empty .gitkeep is clearly a placeholder to "keep" the directory in git.
    – Phil
    Apr 12, 2021 at 17:45
  • 2
    I believe that is your point of view not of all Apr 13, 2021 at 4:31
  • 2
    To be clear, your point of view is that it's OK (or a good idea) to extend the official .gitignore file purpose so that it doubles up as a placeholder when empty? Like I said, I cannot disagree more. Perhaps you could edit your answer to explain your reasoning or back up your point of view? Right now it is a baseless answer and fully deserves my downvote.
    – Phil
    Apr 14, 2021 at 11:07
  • 1
    Ah, I realize I have stumbled in to a conventions war. I just noticed the comments on the accepted answer. Maybe there are two conventions here, and maybe each has their own number of followers so that popularity differs. I would still like to hear the arguments FOR this convention because maybe I will change my mind.
    – Phil
    Apr 14, 2021 at 11:18
  • 4
    I see that. But can you show in the official Git docs where it directs people to add empty .gitignore files?
    – Phil
    Apr 19, 2021 at 23:21
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.