Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between .gitignore and .gitkeep?

Tags:

git

gitignore

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.

like image 282
Matty Avatar asked Aug 29 '11 12:08

Matty


People also ask

What is Gitkeep and Gitignore?

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

What does a Gitkeep file do?

A GITKEEP file is an empty file that Git users create so that a Git repository preserves an otherwise empty project directory. By convention, empty directories that contain no files are not committed to Git repositories.

What is a .gitignore file used for?

gitignore file tells Git which files to ignore when committing your project to the GitHub repository. gitignore is located in the root directory of your repo.

How does .gitignore work?

A . gitignore file is a plain text file where each line contains a pattern for files/directories to ignore. Generally, this is placed in the root folder of the repository, and that's what I recommend. However, you can put it in any folder in the repository and you can also have multiple .


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

like image 93
Wooble Avatar answered Oct 12 '22 15:10

Wooble


.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 
like image 45
sjas Avatar answered Oct 12 '22 16:10

sjas