Checking a Directory Into git
Every so often you need to make sure a directory is created when you checkout a git repository. For example, if it's the output directory for a process like grunt/gulp. Because Git tracks files and not directories it doesn't make this easy but there is a way around.
For example let's make a new repo with a directory we need to be created but don't want to include the files (emptyDir) and another folder that will contain version tracked files.
$ mkdir CheckingDirectoryInGit/
$ cd CheckingDirectoryInGit/
$ git init
Initialized empty Git repository in /blah/CheckingDirectoryInGit/.git/
$ mkdir emptyDir
$ mkdir fullDir
$ touch fullDir/item.txt
If we check git status
we'll see that it's only going to add the directory with the file in it:
$ git status -u
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
fullDir/item.txt
nothing added to commit but untracked files present (use "git add" to track)
However we can create a .gitignore file that includes everything in that folder ('*') and then exclude the .gitignore file so it's included in the repo.
$ echo '*' > emptyDir/.gitignore
$ echo '!.gitignore' >> emptyDir/.gitignore
Note: You could also create a .gitignore file with those two lines but I though this was easier to read.
Now when we check git status
we see that the .gitfile is included:
$ git status -u
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
emptyDir/.gitignore
fullDir/item.txt
nothing added to commit but untracked files present (use "git add" to track)
And we can add another file and it won't be included:
$ touch emptyDir/style.css
$ git status -u
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
emptyDir/.gitignore
fullDir/item.txt
nothing added to commit but untracked files present (use "git add" to track)
Scott Keck-Warren
Scott is the Director of Technology at WeCare Connect where he strives to provide solutions for his customers needs. He's the father of two and can be found most weekends working on projects around the house with his loving partner.
Top Posts
- Working With Soft Deletes in Laravel (By Example)
- Fixing CMake was unable to find a build program corresponding to "Unix Makefiles"
- Upgrading to Laravel 8.x
- Get The Count of the Number of Users in an AD Group
- Multiple Vagrant VMs in One Vagrantfile
- Fixing the "this is larger than GitHub's recommended maximum file size of 50.00 MB" error
- Changing the Directory Vagrant Stores the VMs In
- Accepting Android SDK Licenses From The OSX Command Line
- Fixing the 'Target class [config] does not exist' Error
- Using Rectangle to Manage MacOS Windows