Summary
To discard changes to a file, enter the following command in your terminal:
git checkout -- file-name
Details
The above command only works when you’ve modified a file but haven’t staged it yet.
As an example, if you wanted to discard the changes you’ve made to the home page of your website (index.html), you could use the following command:
git checkout -- index.html
Tip: Git will remind you about this command when you use git status
and the file hasn’t been staged yet. You should see the following message print out when you use git status
:
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
Exercises
Write some code in index.html
. Then use the following command to discard the changes.
git checkout -- index.html
References
Undoing Things from the Pro Git book