# Git: all about adding files to the staging area

Tricks and tips for adding files to the staging area in Git, using globs and
limiting the scope of add, etc.

Adding files in git. The following quote is grabbed from the
[Git documentation](http://git-scm.com/book/en/Getting-Started-Git-Basics#The-Three-States):

> Git has **three** main states that your files can reside in: committed,
> modified, and staged. **Committed** means that the data is safely stored in
> your local database. **Modified** means that you have changed the file but
> have not committed it to your database yet. **Staged** means that you have
> marked a modified file in its current version to go into your next commit
> snapshot.

The staging area is a useful feature unique to Git. The act of `add`ing, means
adding to the staging area. If Git is new to you, the
[beginner's guide to the basic Git workflow](/git-beginners-guide) puts these
commands in context.

## The basics of adding files to the staging area

```shell
# Add a single file
$ git add <file>

# It can be a path also
$ git add path/to/a/<file>
```

Add all the changed files from all sub directories:

```shell
$ git add .
```

If you’re in the project root, the previous command will add everything from all
the directories. If you’re in, say, `projectroot/js` it’ll only affect the files
in the `js` directory. This comes really handy when there are tons of new files
to add in a given directory.

## Adding deleted files

When `git add .` is issued, deleted files won’t be added to the commit (or the
deletion of the file is not added to the commit). You can use the `-u` flag,
short for `--update`, to do that.

From [kernel.org](https://www.kernel.org/pub/software/scm/git/docs/git-add.html)
(emphasis mine):

> [...] That means that it will **_never stage new files_**, but that it will
> stage modified new contents of tracked files and that it will **_remove files
> from the index if the corresponding files in the working tree have been
> removed_**.

This will add all deleted files:

```shell
$ git add -u
```

The previous command affects the whole working tree, all files in all folders
are added, despite the current working directory.

The scope of this command can be limited by navigating to the wanted directory,
and issue the following command, note the dot:

```shell
$ cd foo/
$ git add -u .
```

If you want to do `add .` and `add -u` in the same command, you might go:

```shell
$ git add -u . && git add -u
```

But there’s a built way to do that: `-A` short for `--all`.

```shell
$ git add -A
```

Interesting
[thread](https://stackoverflow.com/questions/572549/difference-between-git-add-a-and-git-add)
on this.

## Using Globs

[Globbing](http://en.wikipedia.org/wiki/Glob_%28programming%29) is possible when
adding files. Globs are a bit like regex, but easier to understand and more
limited.

The below code adds all files ending with `.js` in the `javascript` directory:

```shell
$ git add javascript/*.js
```

Or add all possible files in plugins dir:

```shell
$ git add plugins/*
```

You get the point.

Also,
[see my post on removing files in Git](/git-removing-files-from-the-staging-area-and-the-tree).
