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

bookReverting Changes before Staging

Sometimes you may modify the working tree but decide to discard those changes before staging them.
Now explore methods for reverting changes in Git before they are added to the staging area.

Discarding Changes with git restore

The git restore command is a versatile tool for discarding changes in your working tree. To revert changes in modified files before staging, you can use this command as follows:

git restore <file>

Where <file> is the name of the files or its path relative to the project directory. This command reverts the specified file to the version that is staged. If the staging area is empty, it reverts the file to the version in the latest commit.

Reverting to Staged Version

First, create a new file named recipe.txt containing a simple recipe for boiled eggs using the echo command:

Creating the recipe.txt file

Now add this file and modify it by appending another line with the next step of the recipe:

Adding and modifying the recipe.txt file

Oops, the wrong operator was used β€” > (which overwrites the file) instead of >> (which appends a new line). Before fixing it, check the status of the working tree and staging area:

Checking status

As you can see, the initial version of the file is staged, so revert the file to this version and check the status of the working tree and staging area again:

Restoring file to the staged version

As you can see, our working tree is now clean, so the changes were undone, and the file was reverted to the staged version.

Creating and staging the file
Reverting to staged version

Reverting to the Latest Commit

First, commit the creation of the recipe.txt file:

Committing the recipe.txt file

The commit is successful. Now add another line to the file with the next step of the recipe and check the status of the working tree:

Modifying the recipe.txt file

Oops, another mistake occurred β€” the added step should be 7, not 10.
No problem; you can revert this change to the latest commit.
First, check the latest commit:

The latest commit

This commit is exactly the one needed, so restore the changes to this version and verify that the working tree is clean:

Restoring to the latest commit

The working tree is clean which means that our changes were undone, and the file was restored to the version of the latest commit.

Reverting to the latest commit
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 1

Ask AI

expand

Ask AI

ChatGPT

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

Suggested prompts:

How does git restore know which version to revert to?

What happens if I use git restore on a file that hasn't been staged or committed yet?

Can I use git restore to undo changes in multiple files at once?

Awesome!

Completion rate improved to 3.57

bookReverting Changes before Staging

Swipe to show menu

Sometimes you may modify the working tree but decide to discard those changes before staging them.
Now explore methods for reverting changes in Git before they are added to the staging area.

Discarding Changes with git restore

The git restore command is a versatile tool for discarding changes in your working tree. To revert changes in modified files before staging, you can use this command as follows:

git restore <file>

Where <file> is the name of the files or its path relative to the project directory. This command reverts the specified file to the version that is staged. If the staging area is empty, it reverts the file to the version in the latest commit.

Reverting to Staged Version

First, create a new file named recipe.txt containing a simple recipe for boiled eggs using the echo command:

Creating the recipe.txt file

Now add this file and modify it by appending another line with the next step of the recipe:

Adding and modifying the recipe.txt file

Oops, the wrong operator was used β€” > (which overwrites the file) instead of >> (which appends a new line). Before fixing it, check the status of the working tree and staging area:

Checking status

As you can see, the initial version of the file is staged, so revert the file to this version and check the status of the working tree and staging area again:

Restoring file to the staged version

As you can see, our working tree is now clean, so the changes were undone, and the file was reverted to the staged version.

Creating and staging the file
Reverting to staged version

Reverting to the Latest Commit

First, commit the creation of the recipe.txt file:

Committing the recipe.txt file

The commit is successful. Now add another line to the file with the next step of the recipe and check the status of the working tree:

Modifying the recipe.txt file

Oops, another mistake occurred β€” the added step should be 7, not 10.
No problem; you can revert this change to the latest commit.
First, check the latest commit:

The latest commit

This commit is exactly the one needed, so restore the changes to this version and verify that the working tree is clean:

Restoring to the latest commit

The working tree is clean which means that our changes were undone, and the file was restored to the version of the latest commit.

Reverting to the latest commit
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 1
some-alt