|
| 1 | +# Git Clean: Remove Untracked Files and Directories |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +> Remove untracked files and directories from your repository. |
| 6 | +
|
| 7 | +`git clean` is a powerful command that helps remove untracked files and directories from your repository. It is particularly useful when you want to reset your working directory without affecting committed files. |
| 8 | + |
| 9 | + |
| 10 | +#### Command |
| 11 | +```sh |
| 12 | +git clean |
| 13 | +``` |
| 14 | + |
| 15 | +#### Flags |
| 16 | +- `-n`: Shows what will be deleted without actually deleting anything. |
| 17 | +- `-f`: Forces deletion of untracked files. |
| 18 | +- `-d`: Deletes untracked directories. |
| 19 | +- `-i`: Interactive mode to selectively delete files. |
| 20 | +- `-x`: Removes ignored and untracked files. |
| 21 | +- `-X`: Removes only ignored files. |
| 22 | + |
| 23 | + |
| 24 | +#### Examples |
| 25 | +- **Preview what will be deleted (dry run).** |
| 26 | + |
| 27 | + |
| 28 | +```sh |
| 29 | +git clean -n -d |
| 30 | +``` |
| 31 | +- **Delete all untracked files.** |
| 32 | + |
| 33 | + |
| 34 | +```sh |
| 35 | +git clean -f |
| 36 | +``` |
| 37 | +- **Delete all untracked files and directories.** |
| 38 | + |
| 39 | + |
| 40 | +```sh |
| 41 | +git clean -f -d |
| 42 | +``` |
| 43 | +- **Interactive mode for selective deletion.** |
| 44 | + |
| 45 | + |
| 46 | +```sh |
| 47 | +git clean -i |
| 48 | +``` |
| 49 | + |
| 50 | + |
| 51 | +#### Steps |
| 52 | +1. Preview deletions: `git clean -n -d` |
| 53 | +2. Delete untracked files: `git clean -f` |
| 54 | +3. Delete untracked files and directories: `git clean -f -d` |
| 55 | +4. Interactive deletion: `git clean -i` |
| 56 | +5. Remove ignored and untracked files: `git clean -f -x` |
| 57 | +6. Remove only ignored files: `git clean -f -X` |
| 58 | + |
| 59 | + |
| 60 | +#### Prerequisites |
| 61 | +- Make sure you have committed all important changes. |
| 62 | + |
| 63 | + |
| 64 | +#### Warnings |
| 65 | +- ⚠️ This will permanently delete files! Always use `-n` before `-f`. |
| 66 | +- ⚠️ Be cautious with `-x` and `-X` as they remove ignored files, which might include config files. |
| 67 | + |
| 68 | + |
| 69 | +#### Links |
| 70 | +- [Official Docs](https://git-scm.com/docs/git-clean) |
| 71 | + |
| 72 | + |
| 73 | +#### Tags |
| 74 | +`clean`, `untracked`, `workspace` |
| 75 | + |
| 76 | +#### Author |
| 77 | +mike-rambil |
| 78 | + |
| 79 | +#### Last Updated |
| 80 | +2024-06-10 |
0 commit comments