git does not allow you to add a blank folder in your git repository but you can add an empty directory (technically it’s not empty) by adding a gitignore or gitkeep file inside the empty directory as shown below.
Note: You do not have to use gitignore file, this can be any file even an empty file.
Example:
Create a new local git repository:
1 2 |
## Create a new local git repository git init myrepo && cd myrepo |
Create your 1st git commit:
1 2 3 4 |
## add a new file and commit echo "hello world" > file1 git add file1 git commit -m "commit1" |
Create an empty directory:
1 2 |
## Create an empty directory mkdir mydir |
Check git status:
1 2 3 4 |
## Check git status git status ## On branch master ## nothing to commit, working tree clean |
Observe, git has not detected any change in your working tree. Now let us create a gitingore file inside this empty directory that excludes itself
Create a gitignore file inside the empty folder that exclude itself:
1 2 |
## add a .gitignore file inside the empty folder that exclude itself echo -e '#include everything\n*\n#except\n!.gitignore' > mydir/.gitignore |
Check git status:
1 2 3 4 5 |
## Check git status git status ## On branch master ## Untracked files: ## mydir/ |
Add the blank folder to git:
1 2 3 |
## add and commit the changes git add . git commit -m "commit2" |