Using git, how do you reset the working tree (local file system state) to the state of the index ("staged" files)?

Git

Git Problem Overview


Situation:

  1. Edit files
  2. Add files to the index with git add (these files are now "staged")
  3. Edit more files

Now we have three different states: the state of HEAD (which points to the last commit), the state of the index (which includes all added, or "staged" files) and the state of the working tree (the unstaged, local file system state). What is the command to undo changes in the working tree so that it matches the state of the index?

Git Solutions


Solution 1 - Git

I tend to use git checkout . which discards all changes from the working directory down. This makes a difference if you're not at the root of the repository.

This command doesn't remove newly created files which is usually a good thing. If you need to do this then you can use git clean as well.

Solution 2 - Git

You can use git stash save --keep-index to do this. After saving the stash, you can use git stash drop if you don't want to keep it around.

Solution 3 - Git

You can use git-checkout-index (git checkout-index). Be aware that you need to add

  • -f to force it to overwrite existing files, or
  • -f -a to enforce overwriting all paths in the index.

Solution 4 - Git

git checkout :/ discards all changes in the working tree and replaces it with what's in the index, regardless of the current working directory.

https://git-scm.com/docs/gitglossary#Documentation/gitglossary.txt-aiddefpathspecapathspec

Solution 5 - Git

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:

  1. this great help from @Peter Tillemans
  2. my answer where I needed these commands to do a --hard or --soft git reset by path
  3. [my answer, which contains "All about checking out files or directories in git"] https://stackoverflow.com/questions/2364147/how-to-get-just-one-file-from-another-branch/65258783#65258783

Solution 6 - Git

Use the new git restore command.

Restore the working tree from the index:

git restore .

Restore a file in the working tree from the index:

git restore myFile

source: https://git-scm.com/docs/git-restore

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhaydenmuhlView Question on Stackoverflow
Solution 1 - GitCB BaileyView Answer on Stackoverflow
Solution 2 - GitGreg HewgillView Answer on Stackoverflow
Solution 3 - GitPeter TillemansView Answer on Stackoverflow
Solution 4 - GitmusiphilView Answer on Stackoverflow
Solution 5 - GitGabriel StaplesView Answer on Stackoverflow
Solution 6 - GitAntoine WeberView Answer on Stackoverflow