The other answers I don't think capture the full parts. Here's what you need:
Just the commands:
git checkout-index -fa
# See the WARNING below before running this command.
git clean -fd
With detailed comments:
Note: Run git status
. Changes shown in green are in your index. These are "staged" changes. Changes shown in red are in your working tree, or local file system, but NOT in the index. These are "unstaged" changes. Calling git checkout-index -fa
forces your working tree to match your index, so git status
will no longer show those changes in red after running that command, unless it is an entirely new file you have in your working tree, in which case git clean -fd
is required to remove/delete it.
# 1. 'f'orce checkout 'a'll paths from the index (staged/added files) to the
# working tree (local file system)
git checkout-index -fa
# 2. 'f'orce clean (remove) all files and 'd'irectories which are in the working
# tree but NOT in the index. WARNING WARNING WARNING: this is a destructive
# command and cannot be undone. It is like doing `rm` to remove files.
# First, make sure no changes exist in red when you run `git status` which
# you want to keep.
git clean -fd
From man git checkout-index
:
-f, --force
forces overwrite of existing files
-a, --all
checks out all files in the index. Cannot be used together with
explicit filenames.
See also:
- this great help from @Peter Tillemans
- my answer where I needed these commands to do a
--hard
or --soft
git reset by path
- [my answer, which contains "All about checking out files or directories in git"] How to get just one file from another branch?
git
for years and only discovered yesterday what these terms mean. I think will help reach many more people, as most of the "populace" usinggit
doesn't know these terms, but they do know what their local file system is, whatgit add
is, and what green lines (staged content in the index) means when they look atgit status
. Hopefully you're okay with these changes. – Lingual