Reverting unstaged changes for a specific directory involves two main commands, as Git distinguishes between:
- Tracked files (modified or deleted): Files that Git knows about and are part of your repository.
- Untracked files: New files that Git doesn't know about yet.
Here's how to handle both:
1. Identify Your Changes First (Highly Recommended!)
Before doing anything destructive, always check what changes exist in your target directory:
git status -- <path/to/your/directory>
This will show you:
- "Changes not staged for commit:" (modified, deleted files)
- "Untracked files:" (new files)
2. Reverting Tracked Files (Modified or Deleted)
For files that Git tracks (meaning they were part of a previous commit), you use git restore. This command will revert the files in the specified directory back to their state in the last commit (or staged state, if you've staged changes and want to unstage and revert).
Command:
git restore <path/to/your/directory>
Example:
If you want to revert all unstaged changes in my_project/src/components/button/:
git restore my_project/src/components/button/
What this does:
- Modified files: Their content will be reset to what's in the
HEAD commit.
- Deleted files: They will be restored from the
HEAD commit.
Important Note: git restore by default restores from the HEAD commit. If you had staged changes and then made further unstaged modifications, git restore <file> without a --source will restore from the staged version. To explicitly restore from the HEAD commit, use: git restore --source=HEAD -- <path/to/your/directory> (though for unstaged changes, the simpler command usually suffices).
3. Removing Untracked Files
git restore does not touch untracked files. For new files that Git doesn't know about, you need to use git clean.
Warning: git clean is a powerful and destructive command. It permanently deletes files. Always use the dry-run option first!
Steps:
Step 3.1: Dry Run (See what will be deleted)
git clean -n -d <path/to/your/directory>
-n: Dry run (tells you what would be deleted without actually deleting it).
-d: Include untracked directories as well as files.
Example:
git clean -n -d my_project/src/components/button/
This will output a list of files and directories that would be removed, for example:
Would remove my_project/src/components/button/new_feature.js
Would remove my_project/src/components/button/temp_styles/
Step 3.2: Perform the Clean (Actually delete)
If you're confident after the dry run, execute the command with -f (force):
git clean -f -d <path/to/your/directory>
-f: Force the deletion (required to perform the clean).
-d: Include untracked directories as well as files.
Example:
git clean -f -d my_project/src/components/button/
What this does:
- Deletes any untracked files and directories within the specified path.
- It does not delete files that are in your
.gitignore unless you also use the -x flag (which is usually not desired for a simple revert).
Summary of Steps for a Specific Directory:
-
Inspect:
git status -- <path/to/your/directory>
-
Revert Tracked Changes (Modified/Deleted Files):
git restore <path/to/your/directory>
-
Clean Untracked Files (New Files/Directories):
Legacy Method (using git checkout for tracked files)
Before git restore was introduced in Git 2.23, you would use git checkout for reverting tracked files. It still works, but git restore is clearer about its intent.
git checkout -- <path/to/your/directory>
This command will discard unstaged changes in tracked files within the directory, bringing them back to the HEAD state. However, it still doesn't handle untracked files; you'd still need git clean for those.
It's generally recommended to use git restore for clarity and consistency with Git's newer command naming conventions.