Cancel Staged changes (before committing) in git
Let's learn how to undo changes that have been staged
Step1:Edit file and stage changes
Make changes to the
hello.html file in the form of an unwanted comment
<html> <head> <!-- This is an unwanted but staged comment --> </head> <body> <h1>Hello, World!</h1> </body> </html>
Stage the modified file.
git add hello.html
Step2:Check the status
Check the status of unwanted changes .
$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: hello.html #
Status shows that the change has been staged and is ready to commit.
Step3:Reset the buffer zone
Fortunately, the displayed status shows us exactly what we should do to cancel staged changes.
git reset HEAD hello.html
$ git reset HEAD hello.html Unstaged changes after reset: M hello.html
reset command resets the buffer zone to HEAD. This clears the buffer zone from the changes that we have just staged.
reset command (default) does not change the working directory. Therefore, the working directory still contains unwanted comments. We can use the checkout command from the previous tutorial to remove unwanted changes from working directory.
Step4:Switch to commit version
git checkout hello.html git status
$ git status # On branch master nothing to commit (working directory clean)
Our working directory is clean again.