Git: Undo All Working Dir Changes Including New Files
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 thed
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:
Check Status: Verify the changes you have.
git status
This will show tracked files with modifications, untracked files, and any staged changes.
Discard Changes to Tracked Files:
git checkout -- .
Remove Untracked Files and Directories:
git clean -fd
Unstage Changes (if applicable):
git reset
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 then
(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:
- Discard changes to tracked files with
git checkout -- .
. - Remove untracked files and directories with
git clean -fd
. - 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.
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 usBuild 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.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github