GIT - 4.9 Cancellation of modifications
This lesson covers how to cancel changes at two different levels: first removing them from Git's staging area, then discarding them entirely from the working directory. The starting point is a clean repository on master. We modify the cuckoo file by opening it with our editor, adding a random line of text, saving, and closing:
code coucou.html
Back at the terminal, git status shows the file as modified. We add it to the staging area:
git add coucou.html
Unstaging with git reset HEAD
If we decide we do not want to include those staged changes in the next commit, we can pull them back out of the staging area using git reset with the special pointer HEAD (in uppercase):
git reset HEAD coucou.html
Git replies that the changes have been unstaged. If we open coucou.html again, the random text is still there — the file content has not been touched. git reset HEAD <file> only removes the file from the index; the modifications continue to live in the working directory.
Discarding changes with git checkout -- file
Running git status confirms the file is still modified. To completely ignore those modifications and roll the file back to its last committed state stored in the repository, we use:
git checkout -- coucou.html
After the command, git status shows a clean working directory. Opening coucou.html again confirms the change has disappeared — the file is back to its previous saved version. The two commands together give a clean undo path: git reset HEAD to back out of the staging area, then git checkout -- to discard the actual working-directory edits. Use the second one carefully: there is no further safety net once a change is checked out.
Summary
This lesson demonstrates how to cancel and undo modifications in Git at different stages of the workflow. Covers removing changes from the staging area using `git reset HEAD` to unstage files, and completely discarding modifications using `git checkout` to revert to the last commit. The lesson walks through practical examples using `git status` to verify changes at each step.
Key points
- Use `git status` to check which files have been modified in your working directory
- Stage changes with `git add` to move them to the staging area
- Use `git reset HEAD <filename>` to unstage changes and remove them from the staging area
- Use `git checkout <filename>` to completely discard all modifications and restore the file to its last committed state
- These commands allow you to undo changes at different stages before committing
FAQ
How do I remove changes from the staging area without losing them entirely?
Use the command `git reset HEAD <filename>` to unstage the file. The changes remain in your working directory but are removed from the staging area, allowing you to modify or discard them.
How do I completely discard all modifications and restore a file to its last committed state?
Use the command `git checkout <filename>` to completely undo all changes and revert the file to the last commit. This cannot be undone, so use it carefully.
How can I verify that my modifications have been successfully undone?
Use `git status` after running `git reset HEAD` or `git checkout` to confirm the file state has changed and modifications have been removed.