Sling Academy
Home/DevOps/How to discard local changes in Git (unstage modified files)

How to discard local changes in Git (unstage modified files)

Last updated: January 27, 2024

Introduction

Git is a powerful tool for version control, enabling developers to track changes, create various branches, and collaborate with others. However, while working with Git, it’s quite common to find yourself in a situation where you need to discard local changes that are not yet staged or committed. This tutorial will effortlessly guide you through different methods to unstage and revert modifications, ensuring your project’s integrity is maintained.

What Does Discarding Changes Mean?

Discarding changes in Git means reverting files in your working directory back to their last committed state. If you made changes after your last commit that you’re not satisfied with, you have the option to ‘undo’ these modifications.

Checking Your Current Status

Before you proceed with discarding changes, you should check the status of your files. You can do this by running:

git status

This command will show you the state of your working directory and staging area. It lets you see which changes are staged, unstaged, or untracked.

Discarding Unstaged Changes

To discard changes made to unstaged files, you can use the git checkout command. Here’s how:

git checkout -- <file>

This will revert the specified file to its last committed state. If you want to discard changes made to all modified files, you can use:

git checkout -- .

It’s crucial to note that this command cannot be undone. Make sure you really want to discard the changes before you run it.

Discarding Changes Using Git Restore

In more recent versions of Git, the git restore command has been introduced to make discarding changes easier and more intuitive. To discard changes in a specific file, use:

git restore <file>

If you want to discard changes in all files in the working directory, you run:

git restore .

Again, be careful when using this, as these actions are irreversible.

Dealing with Staged Changes

If you’ve already staged the changes using git add, you will first need to unstage them before you can discard them. To unstage, use the git reset command:

git reset HEAD <file>

To unstage all staged changes, you can run:

git reset

After unstaging the changes, you can proceed to discard them with the git checkout or git restore commands as previously explained.

Discarding All Local Changes Including Staged Changes

In order to reset your branch to exactly match the remote branch, removing all local changes whether they’re staged or not, you can use:

git reset --hard <remote>/<branch>

For instance, if you want to reset to the origin’s master branch, you would run:

git reset --hard origin/master

This will force your local branch to match the remote’s history, so use this command carefully. It is irreversible and you will lose all local changes.

Advanced Tip: Using Git Stash

If you are uncertain about completely discarding your changes or think you might want to revisit them later, Git’s stash feature can be very handy. To stash your changes, simply run:

git stash save "A description of what I'm stashing"

You can later apply the stashed changes with:

git stash pop

Or, you can discard the stash without applying it:

git stash drop

The stash command is a great way to keep your working directory clean without necessarily losing any experimental changes you’ve made.

Conclusion

Git offers various commands to manage and discard your changes effectively, each suited for different scenarios. Whether you’re simply cleaning up your working directory or resetting it to match a remote source, knowing these commands will ensure you can do so with ease and confidence. Just remember, some of these actions are irreversible, so make sure you are certain before you run them.

Next Article: How to check out an old commit in Git

Previous Article: Git Stash Command: The Complete Guide

Series: Git & GitHub Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide