Introduction
🧑💻 New to Git or LabEx? We recommend starting with the Quick Start with Git course.
Git is a version control system that allows developers to track changes in their codebase. Sometimes, developers may make changes to their code that they later decide they don't want to keep. In such cases, it is necessary to discard these changes and revert to the last committed version of the code. This lab will focus on discarding uncommitted changes in Git.
Discard Uncommitted Changes
You have made some changes to your local Git repository, but you have not yet committed them. However, you have decided that you no longer want to keep these changes and want to discard them. The problem is to find a way to discard all uncommitted changes to the current branch.
To complete this challenge, you will use the Git repository named https://github.com/labex-labs/git-playground directory. Follow the steps below:
- Clone the repository to your local machine using the command
git clone https://github.com/labex-labs/git-playground.git. - Navigate to the cloned repository using the command
cd git-playground. - Make some changes to the files in the repository, but do not commit them using the commands
echo "hello,world" > hello.txtandgit add .. - Use the command
git statusto see the changes you have made. - Discard all uncommitted changes using the command
git reset --hard HEAD. - Use the command
git statusagain to confirm that all changes have been discarded.
This is the result of running git status:
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
Summary
Discarding uncommitted changes is a common task in Git. By using the git reset --hard HEAD command, you can reset the local directory to match the latest commit and discard all unstaged changes. This lab has provided an opportunity to practice discarding uncommitted changes in a Git repository.