How Do I Discard Unstaged Changes in Git?

Better Stack Team
Updated on June 24, 2024

To discard unstaged changes in Git, you have a few options depending on your specific requirements:

Discard Changes in a Single File:

If you want to discard changes in a single file and revert it to the last committed version:

 
git checkout -- <file>

Replace <file> with the name of the file you want to discard changes for.

Discard Changes in All Untracked and Modified Files:

If you want to discard changes in all modified and untracked files:

 
git reset --hard

This command will discard all changes in your working directory and staging area and revert your repository to the state of the last commit.

Discard Changes in Modified Files Only:

If you want to keep any new files or changes that have been added to the staging area, but discard changes in modified files:

 
git checkout .

This command will discard changes in all modified files in the working directory, leaving the staging area and new files unchanged.

Note:

  • Be cautious when using git reset --hard as it will discard all changes permanently, including untracked files.
  • Use git checkout -- <file> or git checkout . if you want to selectively discard changes in specific files or only discard changes in modified files while keeping changes in the staging area.
  • Always double-check and ensure that you don't need any of the changes you're discarding, as they cannot be recovered after discarding.
Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.