How to add a Blank Directory to your Git Repository

Sometimes in Git, we want to preserve a directory for use within a repository, but keep it empty of files. There are many reasons why you’d want to do this, but perhaps either the folder is used to store files the person cloning the repository needs to…


This content originally appeared on Level Up Coding - Medium and was authored by Johnny Simpson

Sometimes in Git, we want to preserve a directory for use within a repository, but keep it empty of files. There are many reasons why you’d want to do this, but perhaps either the folder is used to store files the person cloning the repository needs to create, or a script creates custom files to put into that folder.

If we want to create a blank directory, we can do that easily, but it won’t be pushed when we use git push to push to our remote. As such we need to do something slightly different.

Creating a blank directory in a Git Repository

The easiest way to do this is by creating a .gitignore file within the directory you want to maintain.

- index.html
- myRepository <-- Empty directory
- myCss
--- style.css
--- main.css

We want to push myRepository to our git repository. To do that, create a new file in myRepository called .gitignore. In that file, put the following code:

*
*/
!.gitignore

This will ignore all files, folder and subdirectories, but include the .gitignore file itself. That means the file will be pushed, and we can keep the directory while not keeping any of its contents.

Next, just push your git repository as you would usually do by running the following commands:

git add .
git commit -m "Added .gitignore"
git push

Your directory will now persist in your repository, while the contents of the directory will not.


How to add a Blank Directory to your Git Repository was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Johnny Simpson


Print Share Comment Cite Upload Translate Updates
APA

Johnny Simpson | Sciencx (2022-05-11T11:23:20+00:00) How to add a Blank Directory to your Git Repository. Retrieved from https://www.scien.cx/2022/05/11/how-to-add-a-blank-directory-to-your-git-repository/

MLA
" » How to add a Blank Directory to your Git Repository." Johnny Simpson | Sciencx - Wednesday May 11, 2022, https://www.scien.cx/2022/05/11/how-to-add-a-blank-directory-to-your-git-repository/
HARVARD
Johnny Simpson | Sciencx Wednesday May 11, 2022 » How to add a Blank Directory to your Git Repository., viewed ,<https://www.scien.cx/2022/05/11/how-to-add-a-blank-directory-to-your-git-repository/>
VANCOUVER
Johnny Simpson | Sciencx - » How to add a Blank Directory to your Git Repository. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/05/11/how-to-add-a-blank-directory-to-your-git-repository/
CHICAGO
" » How to add a Blank Directory to your Git Repository." Johnny Simpson | Sciencx - Accessed . https://www.scien.cx/2022/05/11/how-to-add-a-blank-directory-to-your-git-repository/
IEEE
" » How to add a Blank Directory to your Git Repository." Johnny Simpson | Sciencx [Online]. Available: https://www.scien.cx/2022/05/11/how-to-add-a-blank-directory-to-your-git-repository/. [Accessed: ]
rf:citation
» How to add a Blank Directory to your Git Repository | Johnny Simpson | Sciencx | https://www.scien.cx/2022/05/11/how-to-add-a-blank-directory-to-your-git-repository/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.