Git: removing files from the staging area and the tree

How to remove stuff in Git.

Remove files from the tree

Scenarion 1: make a commit and notice a stray directory or file that shouldn’t be in the repo. First, add the file to .gitignore, and then:

$ git rm --cached <file-name>
# Globbing is possible as usual
$ git rm --cached *.log

If a directory, use the -r flag:

$ git rm -r --cached directory/

Remove files from the staging area

Scenario 2: if unwanted files were added to the staging area, but not yet committed, then a simple reset will do the job:

$ git reset HEAD file
# Or everything
$ git reset HEAD .

Nuke all made changes for good

Scenario 3: changes in the repo are wanted to be decimated from all eternity:

$ git reset --hard

Careful now, that will remove staged and unstaged changes.

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

$ git checkout -- .

Club-Mate, the beverage →