gitkeep and gitignore in Git

Abdul Jabbar Jan 30, 2023
Git
  1. Difference Between .gitkeep and .gitignore in Git
  2. Commit Empty Folder in Git With gitkeep File
gitkeep and gitignore in Git

Git is used for the management of the source codes among the team. Before we jump into this topic, we must know that Git doesn’t have the capability of tracking empty directories.

If there is an empty directory, Git will ignore it in any condition. It is the feature that we want to use in Git.

As soon as we insert a file in the directory, Git will start tracking that directory in a repository. This file could be anything, or we may say the file consists of anything related to the team’s business.

An empty file is called a .gitkeep file used by Git users to create a Git repository that preserves another empty project directory. Empty directories with no files are not committed to Git remote repositories.

However, we can say that the .gitkeep file in the directory allows Git to maintain the directory in the repository.

Difference Between .gitkeep and .gitignore in Git

The .gitkeep is a file extension that is not a part of the official Git standard but has been introduced by some Git clients through the convention method.

This extension’s name is the .gitkeep that is well deserved because it is the opposite of the .gitignore convention in Git. It is an official file extension of Git that guides the Git which files to be ignored when executing or pushing the code files to the remote repository.

The .gitignore file is used to list files that we want to ignore by the git when searching for untracked files. It can be explained that it will help us keep empty files in Git and ignore them when it searches for untracked files in a repository.

It is not allowed in Git to store empty directories in branches. So if we need to track an empty directory to push, we can accomplish it by creating a .gitkeep file in a folder, which other developers can easily understand in a team.

Adding a file in that folder will allow us to commit and push the directory structure onto the remote repository. If a team is pre-defining a project, they need this folder, but it is clear that there are no files there yet.

We can label it as any name; .gitkeep or .gitkeep is just used as a convention.

Commit Empty Folder in Git With gitkeep File

For having Git push empty directories to the remote repository, we will use the .gitkeep convention by following the steps below.

  • Create an empty folder using the mkdir command in the command prompt.
  • Move into the empty directory using the cd command in the command prompt.
  • We will create the .gitkeep file using the touch .gitkeep command in the command prompt.
  • We will use git add . to update the Git index.
  • After that, perform a git commit using the Git command.
  • Finally, we will push the commit with an empty Git directory to the remote repositories.
$ mkdir empty-folder
$ cd empty-folder
$ touch .gitkeep
$ git add .
$ git commit -m "How to Commit empty folder in Git with gitkeep file."
$ git push origin
Author: Abdul Jabbar
Abdul Jabbar avatar Abdul Jabbar avatar

Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.

LinkedIn