How to Discard Unstaged Changes in Git

Avatar

By squashlabs, Last Updated: Oct. 28, 2023

How to Discard Unstaged Changes in Git

To discard unstaged changes in Git, you can use the git checkout command or the git restore command. Both commands allow you to revert changes in your working directory to the last committed state.

Using git checkout

1. Open a terminal or command prompt.

2. Navigate to the root directory of your Git repository.

3. Run the following command to discard all unstaged changes in your working directory and revert to the last committed state:

git checkout .

4. Alternatively, if you only want to discard changes in a specific file or directory, you can specify the path in the command:

git checkout path/to/file

5. After running the command, Git will overwrite the changes in your working directory with the last committed version of the file(s).

Related Article: How to Fix Git Error: Could Not Read From Remote Repository

Using git restore

1. Open a terminal or command prompt.

2. Navigate to the root directory of your Git repository.

3. Run the following command to discard all unstaged changes in your working directory and revert to the last committed state:

git restore .

4. Alternatively, if you only want to discard changes in a specific file or directory, you can specify the path in the command:

git restore path/to/file

5. After running the command, Git will overwrite the changes in your working directory with the last committed version of the file(s).

Alternative Approaches

1. Instead of discarding all unstaged changes, you can use the git stash command to temporarily save your changes and revert them later. This can be useful if you're not ready to discard the changes permanently but want to switch to a different branch or work on a different task. To stash your changes, run the following command:

git stash save "Your stash message"

2. To apply the stashed changes back to your working directory, use the git stash apply command:

git stash apply

Best Practices

1. Before discarding any changes, make sure you have a backup or a way to recover the discarded changes if needed. Once the changes are discarded, they cannot be easily recovered.

2. Use Git's version control features effectively to track and manage your changes. Committing changes frequently and using branches can help you avoid the need to discard changes in the first place.

3. If you're unsure about the changes you want to discard, use the git status command to see the status of your working directory and the changes you have made.

More Articles from the Git Tutorial: From Basics to Advanced Concepts series:

How to Delete a Git Branch Locally and Remotely

Deleting a Git branch both locally and remotely is essential for maintaining a clean and organized Git repository. This article provides simple steps… read more

How to Undo Last Commits in Git

Undoing the most recent local commits in Git is a common task for software developers. In this article, you will learn simple methods to easily undo … read more

How to Push Changes to a Remote Repository with Git Push

Learn how to use git push to send your local code changes to a remote repository. Understand remote repositories, set them up, and push changes effic… read more

How To Combine My Last N Commits In Git

Learn how to combine multiple commits into a single one using Git's squash feature. Discover the potential reasons for combining commits and explore … read more

How to Revert a Pushed Merge Commit in Git

Reverting a pushed merge commit in Git can be a daunting task, but with the right approach, it can be done efficiently. In this article, we provide a… read more

How To Use Git Pull Rebase

Git pull rebase is a useful tool in your Git workflow for merging code efficiently and avoiding unnecessary merge commits. This article explores why … read more

How to Fix a Git Detached Head

Solving the issue of a Git detached head in your repository can be a simple task. This article provides a guide with two methods to fix the problem. … read more

How To Find The Original URL of a Local Git Repository

In this article, we will guide you on how to find the original URL of a local Git repository. By using the 'git show origin' command or checking the … read more

How to Update Branches in Git using Git Force Pull and Git Pull

Updating branches in Git is essential for staying up-to-date with the latest changes in your codebase. This article will teach you how to update bran… read more

How to Merge Multiple Commits as Single Squashed Commit

Combining multiple commits into a single squashed commit can help streamline your Git workflow. This article provides a step-by-step guide on using G… read more