Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Reverting Changes before Staging | Änderungen Rückgängig Machen
Git Essentials

Reverting Changes before Staging

Swipe um das Menü anzuzeigen

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:

echo "1. Prepare the Eggs
2. Boil Water
3. Place Eggs in the Saucepan
4. Boil the Eggs
5. Reduce Heat
6. Cook for 6-7 minutes" > recipe.txt
Creating the recipe.txt file

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

git add recipe.txt
echo "7. Transfer eggs to ice water or run cold water over them" > recipe.txt
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:

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

git restore recipe.txt
git status
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:

git commit -m "Add a file with a simple recipe for boiled eggs"
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:

echo "10. Transfer eggs to ice water or run cold water over them">> recipe.txt
git status
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:

git log -n 1
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:

git restore recipe.txt
git status
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
question mark

What does the git restore <file> command do before staging changes?

Wählen Sie die richtige Antwort aus

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 1

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Abschnitt 3. Kapitel 1
some-alt