Git

Difference Between gitkeep and gitignore in Git

Git is a development tool utilized among teams to manage the source code and handle projects easily. However, it does not have the capability of tracking empty directories, so if users create an empty directory, it does not track such a directory and ignores it.

This blog will explain the difference between gitkeep and gitignore in Git.

What are gitkeep and gitignore in Git?

gitkeep is used to add an empty directory into the Git repository; however, it is not a built-in feature of Git. For instance, sometimes, users create empty directories and want to add them to the repository. To do so, it is required to create a “.gitkeep” file underneath the empty directory. After doing so, it will become part of the Git repository.

On the other hand, gitignore lists files that Git ignores when it looks for untracked files. If users want to hide or protect any sensitive information regarding their projects, such as credentials, they can save them as “.gitignore” files.

Have a look at the difference between gitkeep and gitignore in Git.

Difference Between gitkeep and gitignore in Git?

Here, we have enlisted the main difference between gitkeep and gitignore:

gitkeep gitignore
gitkeep is not a built-in feature of Git as it is only used to keep empty directories. gitignore is used for listing the ignored files
gitkeep is utilized to track the empty directories in Git. It is used to save sensitive information.

Let’s check out the below-provided sections to comprehend the use of gitkeep and gitignore in Git.

How to Use gitkeep in Git?

To create an empty folder in Git with gitkeep, follow the below instructions.

Step 1: Launch Git Bash

Open the Git Bash terminal in your system with the help of the “Startup” menu:


Step 2: Create Empty Folder

Create an empty folder in the Git Repository with the “mkdir” command:

$ mkdir nill-folder

 

Step 3: Navigate to Folder

Next, execute the “cd” command to move into the created empty folder:

$ cd nill-folder

 

Step 4: Create .gitkeep File

Create a “.gitkeep” file within the empty folder with the help of the provided command:

$ touch .gitkeep

 

Step 5: Add File

Run the “git add .” command to add a file and update the index of Git for tracking:

$ git add .

 

Step 6: Commit Changes

After that, commit the changes into the Git repository with the “git commit” command:

$ git commit -m "empty folder"

 
Here, “-m” is a flag used to add the commit message inside double quotes:


Step 7: Push Commit

Lastly, we will push the commit with the empty folder to the Git remote repository:

$ git push --set-upstream origin main

 
Below output indicates that we have successfully pushed the commit with empty Git directory:


Let’s check out the next section to understand the use of “gitignore” in Git.

How to Use gitignore in Git?

Check out the below steps to ignore the empty folder while tracking the Git repository using gitignore.

Step 1: Open Git Repository

First, open up the Git repository on your system:


Step 2: Create Empty Folder

Next, right-click inside selected the folder, select the “New” option from the appeared menu, and click on the “Folder” option:


Set the name of the folder as “.gitignore”:


Step 3: Launch Git Bash

Open up the “Git Bash” terminal in your system using the “Startup” menu:


Step 4: Navigate to Directory

Navigate to the Git repository using the “cd” command:

$ cd "C:\Users\nazma\Linux_1"

 

Step 5: Check Status

Lastly, check the status of the Git directory in which we have created empty folder:

$ git status

 
As you see, Git did not list the “.gitignore” folder:


We have explained the difference between gitkeep and gitignore in Git.

Conclusion

An empty folder in the Git repository is known as a “.gitkeep” file which is used to make a Git repository that holds another empty directory. On the other hand, the “.gitignore” is used to save sensitive information which is not revealed while tracking the Git repository. This blog demonstrated the difference between Gitkeep and Gitignore in Git and how to use them.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.