What are the differences between .gitignore and .gitkeep?

Asked 2023-09-20 20:28:11 View 190,125

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.

Answers

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

Answered   2023-09-20 20:28:11

  • 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. - anyone
  • @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. - anyone
  • 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 ;/ - anyone
  • @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. - anyone
  • 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 - anyone

.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

Answered   2023-09-20 20:28:11

  • 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 - anyone
  • Why do you need ! in front of .gitignore ? Is that in order to escape the dot ? - anyone
  • @Will - No, the ! negates the following part, like it usually does in programming. - anyone
  • 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. - anyone
  • 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. - anyone
.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.

Answered   2023-09-20 20:28:11

  • You can have as many .gitignores as you want, if you do not want to specify the full path to every folder every time. - anyone
  • 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 ? - anyone
  • .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 - anyone
  • @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. - anyone
  • @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 - anyone

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

Answered   2023-09-20 20:28:11

  • arguably, the reason for the convention is almost explicitly because of git - anyone
  • @JDPeckham Perforce also needs files present to track a folder. - anyone

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.

Answered   2023-09-20 20:28:11

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.

Answered   2023-09-20 20:28: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. - anyone
  • I believe that is your point of view not of all - anyone
  • 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. - anyone
  • 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. - anyone
  • I see that. But can you show in the official Git docs where it directs people to add empty .gitignore files? - anyone