Situation:
git add
(these files are now "staged")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?
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 .
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.
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.
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 .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With