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.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github