Search code examples
gitwhitelistsubdirectory

Add all whitelisted directories to an index


A git repository contains two directories: A and B1. This is the directory structure:

$ find -name "*.*" -print
./.git/hooks/applypatch-msg.sample
./.git/hooks/commit-msg.sample
./.git/hooks/post-update.sample
./.git/hooks/pre-applypatch.sample
./.git/hooks/pre-commit.sample
./.git/hooks/pre-push.sample
./.git/hooks/pre-rebase.sample
./.git/hooks/prepare-commit-msg.sample
./.git/hooks/update.sample
./A/a.txt
./A/A1/a1.txt
./B/b.txt
./B/B1/b1.txt
./C/c.txt

This is the .gitignore file, it whitelists directories A and B1:

$ more .gitignore
# exclude files and directories in top directory
/*

# include the A directory
!/A

# include the B directory
!/B
# exclude files and directories in B
/B/*
# include the B/B1 directory
!/B/B1

The gitignore file is based on last example in http://git-scm.com/docs/gitignore

From inside a whitelisted directory, how to add all whitelisted directories to the index? This does not work:

$ git add / --dry-run
fatal: /: '/' is outside repository

This works from the top directory:

$ git add . --dry-run
add 'A/A1/a1.txt'
add 'A/a.txt'
add 'B/B1/b1.txt'

But from inside a whitelisted directory only that directory is added to the index (B1 is missing):

$ cd A
A$ git add . --dry-run
add 'A/A1/a1.txt'
add 'A/a.txt'

Similar results with:

A$ git add * --dry-run
add 'A/A1/a1.txt'
add 'A/a.txt'

I want a command to add all whitelisted directories from any whitelisted directory so I don't forget one.


Solution

  • With modern Git you can use :/ "top dir" pathspec:

    $ git add :/