Adding all untracked files to Git conditionally

Adding a lot of untracked files in a Git working copy can be a pain. When I want move control over “git add *” or “git add .”, I sometimes run the following and pipe to xargs (and optionally use grep to filter my selection of files). This allows you conditionally add files based on your grep.

# review file modifications
$ git status
#
# ...snip...
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	../all/modules/contrib/examples/ajax_example/ajax_example.test
#	../all/modules/contrib/examples/contextual_links_example/
#	../all/modules/contrib/examples/examples.info
#	../all/modules/contrib/examples/examples.module
#	../all/modules/contrib/examples/form_example/
#	../all/modules/contrib/examples/pager_example/
#	../all/modules/contrib/examples/tablesort_example/
#	../all/modules/contrib/examples/theming_example/

# add all untracked files
$ git status -s | grep ^?? | awk '{print $2}' | xargs -i git add '{}'

For example if you only wanted to add jpg files:

$ git status -s | grep ^?? | awk '{print $2}' | grep -i \.jpg$ | xargs -i git add '{}'

And on Mac OSX:

$ git status -s | grep ^?? | awk '{print $2}' | grep -i \.jpg$ | xargs -I{} git add '{}'

Updated: