# Git: removing files from the staging area and the tree

Removing stuff with git: just remove any file, remove files from the staging
area, how to remove files that were not supposed to be added, and how to remove
everything.

## Simply remove a file

**Scenario 1:** an unused file needs to be removed:

```shell
$ git rm <file-name>
$ git commit -m "Remove old file"
```

**What’s the difference between `rm` and `git rm`?** Not much: `rm foo.txt`,
will remove the file, and `git status` will show `deleted: foo.txt`. But
`git rm foo.txt` will remove the file and **add it to the staging area**.

## Remove files from the staging area

**Scenario 2:** wrong files were added, but they were not yet committed, then a
simple `reset` will remove the files from the staging area, but _doesn’t
actually delete_ the files. This is the same staging area described in the
[Git guide to adding files](/git-all-about-adding-files-to-the-staging-area):

```shell
$ git reset HEAD <file-name>

# Or everything
$ git reset HEAD
```

## Remove files that should’ve been ignored

**Scenario 3:** make a commit and notice a stray directory or a file (for
example `.DS_Store`) that should have been ignored in the first place, i.e.:
**make git forget already committed files**.

First, add the file to the project’s `.gitignore`, and then neutralize the
cache:

```shell
$ git rm --cached <file-name>

# Globbing is possible as usual
$ git rm --cached *.log
```

If a directory, use the `-r` flag:

```shell
$ git rm -r --cached directory/
```

## Nuke all made changes for good

**Scenario 4:** changes in the current branch are wanted to be decimated from
all eternity:

```shell
$ git reset --hard
```

Careful, that will remove **staged and _unstaged_ changes**.

To only remove unstaged changes in the current working directory, use:

```shell
$ git checkout -- .
```

Also, a given amount of commits can be reset. The following will destroy the
last two commits completely. For accidentally deleted files, use the recovery
steps in
[this Git guide to getting deleted files back](/git-deleted-a-bunch-of-files-accidentally-from-a-repository-how-to-get-them-back):

```shell
$ git reset HEAD~2 --hard
```

Soft reset is also possible, which is not really removing any files. This way
the commits are removed, **but the changed files are kept**:

```shell
$ git reset HEAD~2 --soft
```

After that, git shows a dirty tree.
