Matty
Matty

Reputation: 34413

What are the differences between .gitignore and .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.

Upvotes: 2654

Views: 646953

Answers (8)

Vivek Kumar
Vivek Kumar

Reputation: 2889

.gitignore is all about ignoring files and .gitkeep is a simple placeholder

+----------+----------------------------------+-----------------------------------+
| Aspect   | .gitignore                       | .gitkeep                          |
+==========+==================================+===================================+
| Purpose  | Tells Git which files and        | A placeholder file often used     |
|          | directories to ignore            | to ensure an otherwise empty      |
|          | (exclude) from version control   | directory is included in Git      |
+----------+----------------------------------+-----------------------------------+
| Scenario | List patterns or specific        | Create a file named .gitkeep      |
|          | filenames that should not        | inside an empty folder so         |
|          | be tracked by Git (e.g.,         | Git will include that folder      |
|          | build artifacts)                 |                                   |
+----------+----------------------------------+-----------------------------------+
| How It   | The patterns in .gitignore       | .gitkeep is tracked like a        |
| Works    | match files and folders,         | normal file but its content       |
|          | so Git ignores them when         | is usually blank                  |
|          | you commit                       |                                   |
+----------+----------------------------------+-----------------------------------+
| Impact   | Allows you to configure          | Ensures a directory (which        |
| on Files | which files remain               | might otherwise remain empty)     |
|          | untracked, so they never         | is kept in the repository         |
|          | appear in commits                |                                   |
+----------+----------------------------------+-----------------------------------+
| Scope    | Affects any files matching       | Only acts as a placeholder file   |
|          | the patterns declared in         | in the directory where it is      |
|          | that .gitignore                  | placed                            |
+----------+----------------------------------+-----------------------------------+
| Usage    | Avoid cluttering the repository  | Keep folders like logs/ or temp/  |
|          | with OS or build files like      | in Git, even if the folder        |
|          | node_modules/ or *.log           | is empty in your repo             |
+----------+----------------------------------+-----------------------------------+

Upvotes: 1

dain
dain

Reputation: 6689

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.

Upvotes: 1

krishnaacharyaa
krishnaacharyaa

Reputation: 24912

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.

Upvotes: 6

sjas
sjas

Reputation: 19668

.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

Upvotes: 489

Wooble
Wooble

Reputation: 89847

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

Upvotes: 4597

Sohel Ahmed Mesaniya
Sohel Ahmed Mesaniya

Reputation: 3450

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.

Upvotes: 2

rupakg
rupakg

Reputation: 518

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

Upvotes: 10

Jim Munro
Jim Munro

Reputation: 1647

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

Upvotes: 152

Related Questions