Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Git Tutorial
  • Git Exercises
  • Git Basic Commands
  • Git Cheat Sheet
  • Git Interview Questions
  • Git Bash
  • GitHub
  • Git Branch
  • Git Merge
  • Git WorkFlow
  • Git Hooks
  • Git LFS
  • Git Rebase
  • Git Cherry Pick
Open In App
Next Article:
How to Add an Empty Directory to a Git Repository?
Next article icon

How to Add an Empty Directory to a Git Repository?

Last Updated : 23 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Adding an empty directory to a Git repository can be a challenging task, as Git doesn't track empty directories by default. However, it is essential to keep track of empty directories in a repository, especially if you are working in a team or if you want to maintain the directory structure of your project.

Fortunately, there are a few ways to add an empty directory to a Git repository, and in this article, we'll be discussing two of the most commonly used methods.

Method 1: Using a placeholder file

The easiest way to add an empty directory to a Git repository is to create a placeholder file in the directory and add it to the repository. This file can be a simple text file, such as .gitkeep or .gitignore, and it acts as a marker to indicate that the directory should be tracked by Git. To do this, follow these steps:

Create a new directory in your local repository. You can do this using a file explorer or by running the following command in your terminal:

mkdir <directory_name>

Create a new file in the directory. For example, you can create a file named .gitkeep. You can do this using a text editor or by running the following command in your terminal:

cd <directory_name>

Add the new file to the Git repository by running the following command:

git add .gitkeep

Commit the changes to the repository with a commit message:

$ git commit -m "Adding empty directory with .gitkeep"

Method 2: Using a .gitignore file

Another way to add an empty directory to a Git repository is to create a .gitignore file in the directory. The .gitignore file is used to specify files or directories that should be ignored by Git. To add an empty directory, follow these steps:

Create a new directory in your local repository. Change your current directory to the newly created directory by running the following command:

$ cd <directory_name>

Create a new file in the directory with the name .gitignore. You can do this using a text editor or by running the following command in your terminal:

$ touch .gitignore

Add the following line to the .gitignore file:

# Ignore everything in this directory
*
# Except this .gitignore file
!.gitignore

Add the .gitignore file to the Git repository by running the following command:

$ git add .gitignore

Commit the changes to the repository with a commit message:

git commit -m "Adding empty directory with .gitignore"

In conclusion, adding an empty directory to a Git repository can be done using either a placeholder file or a .gitignore file. Both methods work well and allow you to keep track of the empty directory in your repository. The method you choose to use will depend on your personal preference and the requirements of your project.

If you are working in a team, it's recommended to have a consistent approach to adding empty directories to the repository, so everyone is on the same page. 


Next Article
How to Add an Empty Directory to a Git Repository?

R

rishabmalhdijo
Improve
Article Tags :
  • Git
  • How To

Similar Reads

    How to Remove Directory From a Git Repository?
    Managing files and directories within a Git repository is a common task, especially when you need to clean up or reorganize your project's structure. Removing a directory from a Git repository involves several steps to ensure that the change is tracked and applied correctly. In this article, we will
    2 min read
    How to Delete a Git Repository
    GAs developers and software teams are collaborating on projects, the usability of version control systems like Git to track changes and manage codebases has increased a lot. Using Git, developers can create and maintain repositories, that store the complete history and versions of projects. However,
    6 min read
    How to Delete a File From a Git Repository?
    Managing files within a Git repository often involves adding new files, modifying existing ones, and sometimes, deleting files that are no longer needed. Deleting a file from a Git repository is a simple process, but it's essential to follow the correct steps to ensure that the repository remains co
    3 min read
    How to Add Code on GitHub Repository?
    GitHub is a powerful platform for hosting and sharing code. Whether you’re working on a solo project or collaborating with others, adding code to a GitHub repository is essential. Here’s a step-by-step guide on how to add your code to a GitHub repository. Steps to Add Code on GitHub RepositoryStep 1
    2 min read
    How To Clone a Repository From Gitlab?
    Cloning a repository in Git involves creating a local copy of a project from a remote server. This allows you to work on the project on your local machine and later push your changes back to the remote repository. GitLab, one of the popular platforms for hosting Git repositories, provides a straight
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences