Before cleaning a repository, make Git show the candidate paths. The commands in this guide all include -n, so they are previews. Start from the repository root and save any open editor buffers before inspecting the files on disk.
git status --short --untracked-files=all
git clean -ndA short status listing and a cleanup preview answer different questions. Status helps identify tracked and untracked work. The cleanup preview shows paths selected by the clean command's options. Read both before deciding what should happen next.
A small example you can compare with your own repository
The checks for this guide used Git 2.55.0 on Windows, an existing commit containing tracked.txt, and these ignore rules:
build/
*.logThe working directory also contained notes.txt, draft notes.txt, drafts/todo.txt, build/result.txt and debug.log. Only the first three paths were ordinary untracked work; the build output and log matched the ignore rules.
For that fixture, the ordinary preview produced:
git clean -nd
Would remove draft notes.txt
Would remove drafts/
Would remove notes.txtEvery example file remained present with identical contents afterwards. The tracked file, commit and staged diff were unchanged too. Your output depends on your paths and ignore configuration; it need not match these example lines.
Choose the preview that matches your question
| Command | What the example selected |
|---|---|
| git clean -nd | Ordinary untracked files and the untracked drafts directory |
| git clean -ndX | Ignored build output and debug.log only |
| git clean -ndx | Both groups of untracked paths, including ignored ones |
The capital X and lowercase x are different options. In the tests, the capital-X preview listed build/ and debug.log but left out notes.txt. The lowercase-x preview listed all five untracked paths or directories. None of these commands changed their bytes.
The official git clean reference defines the dry-run, directory and ignore-selection options. Keep -n in place while comparing the candidate lists. Including -d makes ordinary untracked directories part of the preview.
Restrict the preview to a named path
git clean -nd -- "draft notes.txt"The double hyphen separates options from the path. Quotes keep the filename with spaces together in PowerShell and common Unix shells. In the isolated check, the result named draft notes.txt without selecting the other example paths. That file's bytes were unchanged afterwards.
Use your own path, then read the entire result. A preview is an observation of the current working directory; creating another file or changing ignore rules can change a later result.
Explain a surprising omission
git check-ignore -v -- build/result.txt debug.logThe test output identified the build/ and *.log rules in .gitignore. The verbose check-ignore reference explains the rule and pathname fields. Ignore behavior can also come from other configured ignore sources, so inspect the reported rule rather than assuming it must be in the repository file.
A tracked file is a different case. Use git status to identify its state, and see the tracked-file ignore walkthrough when changing .gitignore has no effect. If your goal is to remove something from the next commit, unstage it while retaining your edits instead.
This guide stops at inspection. Untracked files may have no committed copy for Git to recover. Keep an independent copy of anything important before moving beyond a preview. The tests cover ordinary files and directories, not nested repositories, submodules or recovery after deletion.
No comments:
Post a Comment