git,

Properly remove untracked files in Git

Oct 18, 2022 · 1 min read · Post a comment

You can always rm -rf files and directories from your working tree for sure, or even git reset --hard. However, there is another way, the Git way, a much proper one to do the same thing.

Prerequisites

  • Git

Solution

Dry run for files:

git clean -n

Dry run for directories as well:

git clean -nd

Remove untracked files:

git clean -f

Remove untracked files in a subdirectory:

git clean -f <dir_abs_path>

Note: This will delete the subdirectory as well.

Remove untracked directories too:

git clean -fd

Remove ignored files only:

git clean -fX

Remove ignored and non-ignored files:

git clean -fx

Note: Add d to include subdirectories.

Conclusion

To find more neat Git commands and hacks, simply browse the Git category. Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.

git