Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Reverting Staged Changes | Undoing Changes
Git Essentials
course content

Conteúdo do Curso

Git Essentials

Git Essentials

1. Introduction to Git
2. More Advanced Interaction
3. Undoing Changes
4. Working with Branches in Git

book
Reverting Staged Changes

Sometimes, changes are staged for commit, but upon further consideration, you realize that certain modifications should not be included. Luckily, it is possible to revert staged changes in Git, ensuring that your commits accurately reflect the intended modifications.

Undoing Staged Changes with git restore

The git restore command is a powerful tool for reverting staged changes. To undo modifications in files that have been staged but not yet committed, you can use the following command:

Where <file> is the name of the files or its path relative to the project directory. The --staged flag here "tells" Git to restore the changes in the staging area. After running this command the changes in the <file> will be unstaged.

Note

This command doesn’t revert the changes in the working tree. Instead, it only unstages them.

In case you want to unstage and then revert the changes in the working directory, you can use the following command:

Here is an image to make things clear:

Example Workflow

Suppose we want to add another line containing another step for our recipe:

Unlike in the previous chapter, we made no mistake here, so let’s stage this change:

However, on a second thought before committing, we decided that adding this line to the recipe file makes no sense. Consequently, it’s time to completely revert this change. Let’s first check the status of our working tree and staging area:

This change is indeed staged, so we have to run the git restore command with appropriate flags to fully revert it:

Finally, let’s verify that our working tree and staging area are both clean:

question-icon

Match the actions with the respective commands.

Unstage changes:
Unstage and revert the changes in the working directory:

Revert the changes in the working directory:

Clique ou arraste solte itens e preencha os espaços

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 2
We're sorry to hear that something went wrong. What happened?
some-alt