Git: Undo All Working Dir Changes Including New Files

Better Stack Team
Updated on August 12, 2024

To undo all changes in your working directory, including new (untracked) files, you can use a combination of Git commands to revert everything to the state of the last commit. Here’s how you can achieve this:

Steps to Undo All Working Directory Changes Including New Files

1. Discard Changes to Tracked Files

To undo changes to tracked files (files that have been previously committed), you can use:

 
git checkout -- .

This command will discard changes in all tracked files in your working directory and revert them to their state in the last commit.

2. Remove Untracked Files and Directories

To remove untracked files and directories (new files that haven’t been added to the index), use:

 
git clean -fd
  • f (force): Required to actually delete the files.
  • d (directories): Also removes untracked directories. If you only want to remove untracked files and keep directories, omit the d option.

Be Cautious: This command will permanently delete untracked files and directories, so make sure you don’t need them before running it.

3. Undo Staged Changes (Optional)

If you also have changes that are staged (added to the index but not committed) and want to unstage them, use:

 
git reset

This command will unstage all changes, leaving them in your working directory. You would then use git checkout -- . to discard these changes as described earlier.

Example Workflow

Here’s a step-by-step example to completely undo all changes, including new files:

  1. Check Status: Verify the changes you have.

     
    git status
    

    This will show tracked files with modifications, untracked files, and any staged changes.

  2. Discard Changes to Tracked Files:

     
    git checkout -- .
    
  3. Remove Untracked Files and Directories:

     
    git clean -fd
    
  4. Unstage Changes (if applicable):

     
    git reset
    
  5. Verify: Ensure everything is as you want it.

     
    git status
    

    The output should indicate a clean working directory and no untracked files.

Additional Tips

  • Dry Run of git clean: If you want to see which files would be removed without actually deleting them, you can use the n (dry-run) option:

     
    git clean -fdn
    

    This will list the files and directories that would be deleted.

  • Restoring Files: If you realize you’ve accidentally deleted important files, Git does not provide a direct way to recover them after git clean. In such cases, you might need to use file recovery tools or restore from a backup if available.

Summary

To undo all changes in your working directory, including new files:

  1. Discard changes to tracked files with git checkout -- ..
  2. Remove untracked files and directories with git clean -fd.
  3. Optionally, unstage changes with git reset.

These commands will restore your working directory to the state of the last commit, discarding all modifications and new files.

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