Git Undo All Uncommitted or Unsaved Changes

Better Stack Team
Updated on August 12, 2024

To undo all uncommitted or unsaved changes in Git, you need to handle different types of changes separately: changes in the working directory and changes staged for the next commit. Here’s a comprehensive guide to undoing these changes:

1. Undo Changes in the Working Directory

If you have changes in your working directory that you haven’t staged yet (i.e., changes to files that have been modified but not yet added to the index), you can discard these changes with the following commands:

Discard Unstaged Changes

To discard changes in the working directory, reverting files back to the last committed state:

 
git restore .

or for older Git versions (prior to 2.23):

 
git checkout -- .

This will undo all changes in all tracked files in the working directory. If you want to discard changes for a specific file:

 
git restore <file>

or

 
git checkout -- <file>

2. Undo Staged Changes

If you have staged changes (i.e., changes that you have added to the index using git add), you need to unstage them before you can discard them.

Unstage Changes

To unstage files that have been added to the index but not yet committed:

 
git restore --staged .

or for older Git versions:

 
git reset

To unstage a specific file:

 
git restore --staged <file>

or

 
git reset <file>

3. Undo Both Staged and Unstaged Changes

If you want to discard both staged and unstaged changes and reset the working directory to the last commit:

  1. Unstage All Changes:

     
    git restore --staged .
    
  2. Discard Unstaged Changes:

     
    git restore .
    

or combine these steps:

 
git reset --hard HEAD

4. Remove New Files or Directories

If you have new files or directories that are not yet tracked by Git and you want to remove them:

Remove Untracked Files and Directories

To remove all untracked files and directories:

 
git clean -fd
  • f: Force removal of files.
  • d: Remove untracked directories as well.

To remove only untracked files (leaving directories):

 
git clean -f

To preview which files will be removed before actually deleting them:

 
git clean -n

5. Summary of Commands

  • Discard Unstaged Changes: git restore . or git checkout -- .
  • Unstage Changes: git restore --staged . or git reset
  • Discard Both Staged and Unstaged Changes: git reset --hard HEAD
  • Remove Untracked Files/Directories: git clean -fd
  • Preview Untracked Files for Removal: git clean -n

Important Notes

  • Data Loss Warning: Commands like git restore ., git reset --hard HEAD, and git clean -fd will permanently delete changes and files. Use these commands with caution.
  • Backup: Consider stashing your changes using git stash if you want to save them temporarily before discarding.

By following these instructions, you can effectively manage and undo changes in your Git working directory and index.

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