Discarding staged changes
Now, suppose we've decided to make our Hello, World!
stand out. Let's make it look like a header!
To do this, why don't we wrap the content of hello.html
with the <head>
tags? The result should look like this:
<head>Hello, World!</head>
Wrap the content of hello.html
with the <head> </head>
tags and stage the changes.
Just a sec... Did I say <head> </head>
tags? Oh sorry, of course I meant the <h1> </h1>
tags! Alas, the changes have already been staged.
Before doing anything, let's quickly run git status
to see what's going on.
SUPER HELPFUL TIP: You can use the and arrow keys on your keyboard to navigate through your command history in the terminal. This is a great way to avoid retyping commands.
Run the git status
command to check the status of the repository.
You should see something like this:
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: hello.html
Let's follow the command's advice and unstage the changes, using the git restore
command, with the extra --staged
option.
Run the git restore --staged .
command to clean the staging area.
Let's check the status again.
Run the git status
command to check the status of the repository.
The output should look like this:
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: hello.html
no changes added to commit (use "git add" and/or "git commit -a")
Surprise, surprise! The changes are still present! But at least they are not staged for commit anymore.
To discard them completely, we can run the git restore
command without any options (just like we did in the previous step of the course).
Run git restore .
to clean the working tree.
Let's check the status again.
Run the git status
command to check the status of the repository.
The output should look like this:
On branch main
nothing to commit, working tree clean
And that's it! The changes have been discarded and the working tree is clean again.
In fact, we could have restored the working tree to a clean state in one go. The git restore
command works in two modes: the default --worktree
mode, which restores the working tree, and the --staged
mode, which restores the staging area. We could have used both options at the same time to restore both the staging area and the working tree like this: git restore --staged --worktree .
.
Hi! I'm Alex, creator of GitByBit.
This page is a part of the interactive course about Git version control.
It's a one-of-a-kind course that is integrated into the VS Code code editor. Learning directly in VS Code lets you operate Git exactly as you would in real life, doing real jobs, writing real code.
In addition, the course has access to your actual terminal, so it can point out mistakes, suggest workarounds, etc.
The course is FREE, there are no Ads or other bullshit. There are optional premium add-ons you can purchase, mainly to support my work (regrettably, I have to eat every day), but that's totally up to you.