How to Discard Your Local Changes in Git: An Illustrated Guide

As developers, we’ve all encountered situations where we make changes to our code, only to realize later that those changes were unnecessary or need to be discarded. Git, the powerful version control system, provides a straightforward way to undo or discard local changes and revert to the last committed state. In this blog post, we’ll walk you through the process of discarding local changes in Git using practical examples.

TL;DR - To discard all changes:

git checkout -- .

Before we begin, make sure you have Git installed on your system and have a basic understanding of using Git commands.

Step 1: Check Status

The first step is to check the status of your current repository. Open your terminal or command prompt and navigate to the Git repository you’re working on. Now, run the following command:

git status

This command will display all the changes you have made to your repository, including modified files, untracked files, and staged files.

Step 2: Discard Unstaged Changes

If you want to discard changes made to files that have not been staged (i.e., changes that are still in your working directory), you can use the git checkout command. For example, let’s say we made some changes to a file called “script.js” and want to discard those changes:

git checkout -- script.js

The git checkout -- <filename> command reverts the specified file to the state it was in during the last commit.

Step 3: Discard Staged Changes

Sometimes, you may have already staged some changes that you now wish to discard. To remove staged changes and revert to the previous state, use the git reset command:

git reset HEAD <filename>

For instance, if you staged changes to the “index.html” file and want to discard them, you can use:

git reset HEAD index.html

This command will remove the specified file from the staging area, effectively undoing the last git add command.

Step 4: Discard All Local Changes

In some cases, you may want to discard all local changes at once and revert your working directory to the state of the last commit. You can achieve this using the git checkout command with the . argument:

git checkout -- .

The . indicates the current directory, and the git checkout -- . command discards all local changes across the entire repository.

Step 5: Confirm Discarded Changes

After performing the necessary commands to discard changes, it’s essential to verify that your repository is back to the last committed state. Run the git status command again:

git status

This will show that your working directory is clean, and there are no changes to be committed.

Conclusion

In this blog post, we’ve covered the essential steps to discard local changes in Git. By utilizing the appropriate commands, you can easily revert your working directory to the last committed state, undoing any unnecessary changes along the way. Remember to use these commands with caution, as discarded changes cannot be recovered once they are removed.

So, the next time you find yourself needing to undo some local modifications, remember these simple Git commands, and you’ll be back on track in no time! Happy coding!