Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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?

like image 235
haydenmuhl Avatar asked Jun 24 '10 06:06

haydenmuhl


People also ask

How do I restore a staged file in git?

Staged files are those which go into your next commit. If you accidentally added files to the staged area, you can undo this by typing git restore --staged <file> , so in this case, it would be git restore --staged lib. c .

Does git reset change local files?

Git reset is essentially the opposite of the command git add . It will undo the git add to remove the changed file from version control, and then you can git checkout to undo the changes from the file.

How does git reset work?

To review, git reset is a powerful command that is used to undo local changes to the state of a Git repo. Git reset operates on "The Three Trees of Git". These trees are the Commit History ( HEAD ), the Staging Index, and the Working Directory. There are three command line options that correspond to the three trees.

What is git reset example?

reset is the command we use when we want to move the repository back to a previous commit , discarding any changes made after that commit . After the previous chapter, we have a part in our commit history we could go back to. Let's try and do that with reset .


1 Answers

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.

like image 169
CB Bailey Avatar answered Oct 04 '22 17:10

CB Bailey