How to Discard Unstaged Changes in Git

Avatar

By squashlabs, Last Updated: October 27, 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 Use Git Stash Apply Version

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.

Related Article: How to Stash Untracked Files in Git

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

How to Remove Files From a Git Staging Area

Removing files from Git's staging area is a simple process that can help you manage your repository more efficiently. This article provides practical examples and... read more

How to Move Recent Commits to a New Branch with Git

Guide to relocating recent commits to a new branch using Git commands. Learn two methods: using git branch and git cherry-pick commands, or using the git rebase command.... read more

How to Remove a File From a Git Repository

Deleting files from a Git repository is a common task for software developers. This article provides two methods for removing files: using the git rm command and using... read more

How to Force Overwrite During Git Merge

This article provides a step-by-step guide on how to force overwrite during a Git merge operation. It covers two methods: using the --strategy-option flag and using the... read more

How To Name And Retrieve A Git Stash By Name

Naming and retrieving a Git stash by name is a fundamental skill for effective version control. This article provides a simple guide on how to accomplish this task. It... read more

How to Rename Both Local and Remote Git Branch Names

Renaming Git branch names locally and remotely can be done with ease using a few simple methods. This guide provides step-by-step instructions on how to achieve this... read more