I'd like to only track certain directories in git in a larger project, where most directories will be excluded and only a few will be tracked. So, I wanted to use the not-operators in my .gitignore or excludes file, ja?
Why is this happening:
% mkdir wtfgit
% cd wtfgit
% git init
Initialized empty Git repository in /home/foobar/wtfgit/.git/
% mkdir iwantthesefiles
% touch iwantthesefiles/foo
% mkdir idontwantthesefiles
% touch idontwantthesefiles/bar
% vim .gitignore
% cat .gitignore
*
!iwantthesefiles/
% git add --all
% git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
The "not" syntax works fine for files in the highest directory.
% touch baz
% vim .gitignore
% cat .gitignore
*
!iwantthesefiles/
!baz
% git add --all
% git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: baz
#
How can I get this to give me baz and foo as git tracked files? (Basically, how does the directory NOT-style globbing work at all?! It seems not to.)
Thanks!
Git can only ignore files that are untracked - files that haven't been committed to the repository, yet. That's why, when you create a new repository, you should also create a . gitignore file with all the file patterns you want to ignore.
In case none of the above works try this variation inside .gitignore
Not working for me:
!build/**/*
Working for me:
!build**/*
For some reason this was the only way it worked for me.
To forcefully add folders that are ignored, use:
git add -f my-ignored-folder
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With