What Are the Differences between .gitignore and .gitkeep?
Better Stack Team
Updated on June 24, 2024
.gitignore
and .gitkeep
are two different files used in Git repositories for distinct purposes:
.gitignore
- The
.gitignore
file is used to specify intentionally untracked files that Git should ignore. - It contains a list of file patterns or paths that Git should ignore when performing operations like
git status
,git add
, andgit commit
. - You typically add files like build artifacts, temporary files, logs, and dependency directories to
.gitignore
to prevent them from being accidentally committed to the repository. - Git will not track or commit any files matching the patterns specified in
.gitignore
. .gitignore
does not affect tracked files already in the repository.
.gitkeep
- The
.gitkeep
file is not a standard Git feature but rather a convention used by some projects. - It is an empty file (or sometimes a file with a single line of content) placed in an otherwise empty directory to force Git to track the directory.
- Git does not track empty directories by default. When you try to add an empty directory to Git, it will be ignored.
- By adding a
.gitkeep
file to an empty directory, you signal to Git that the directory should be tracked, even if it's empty. - This convention is often used in projects to maintain directory structures in source control, even if the directories do not contain any files initially.
Note:
.gitignore
is a standard feature of Git, whereas.gitkeep
is a convention used by some projects..gitignore
is used to specify files that Git should ignore, while.gitkeep
is used to force Git to track otherwise empty directories.- Both files serve different purposes and are used in different contexts within a Git repository.
Got an article suggestion?
Let us know