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

bookReverting 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:

git restore --staged <file>

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
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:

git restore --staged --worktree <file>

Here is an image to make things clear:

Modifying and staging a file
Reverting staged changes

Example Workflow

Suppose you want to add another line with the next step of the recipe:

Unlike in the previous chapter, no mistake was made, so stage this change:

Modifying the recipe.txt file

However, after reconsidering before committing, it appears that adding this line to the recipe file is unnecessary. Therefore, completely revert this change. First, check the status of the working tree and staging area:

Checking status

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

Finally, verify that both the working tree and staging area are clean:

Reverting staged changes
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:

Click or drag`n`drop items and fill in the blanks

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 3.57

bookReverting Staged Changes

Swipe to show menu

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:

git restore --staged <file>

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
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:

git restore --staged --worktree <file>

Here is an image to make things clear:

Modifying and staging a file
Reverting staged changes

Example Workflow

Suppose you want to add another line with the next step of the recipe:

Unlike in the previous chapter, no mistake was made, so stage this change:

Modifying the recipe.txt file

However, after reconsidering before committing, it appears that adding this line to the recipe file is unnecessary. Therefore, completely revert this change. First, check the status of the working tree and staging area:

Checking status

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

Finally, verify that both the working tree and staging area are clean:

Reverting staged changes
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:

Click or drag`n`drop items and fill in the blanks

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 2
some-alt