0
0
Gitdevops~15 mins

git restore to discard working changes - Mini Project: Build & Apply

Choose your learning style9 modes available
Using git restore to Discard Working Changes
📖 Scenario: You are working on a project using Git. You accidentally made some changes to a file but want to discard those changes and return the file to the last committed state.
🎯 Goal: Learn how to use the git restore command to discard changes in your working directory safely.
📋 What You'll Learn
Create a file with some content
Modify the file to simulate changes
Use git restore to discard the changes
Verify the file content is restored to the last commit
💡 Why This Matters
🌍 Real World
In real projects, sometimes you make changes you don't want to keep. Using <code>git restore</code> helps you quickly undo those changes without affecting your commits.
💼 Career
Knowing how to discard unwanted changes is essential for developers and DevOps engineers to maintain clean code and avoid mistakes in version control.
Progress0 / 4 steps
1
Create a file with initial content
Create a file called example.txt with the exact content Hello, Git! and add it to the Git repository with git add example.txt and commit with the message Initial commit.
Git
Need a hint?

Use echo to create the file, then git add and git commit to save it.

2
Modify the file to simulate changes
Modify the file example.txt by adding the line This is a change. to simulate uncommitted changes.
Git
Need a hint?

Use echo with >> to append the new line to the file.

3
Discard the changes using git restore
Use the command git restore example.txt to discard the uncommitted changes in example.txt and restore it to the last committed state.
Git
Need a hint?

Use git restore followed by the filename to discard changes.

4
Verify the file content is restored
Use cat example.txt to display the content of example.txt and verify it only contains Hello, Git! without the discarded line.
Git
Need a hint?

Use cat example.txt to see the file content.