0

How can I recursively delete all files from my worktree that are tracked by git, leaving only the untracked files? And if the command would also delete then-empty directories...

This is something like an inverse operation to git clean.

My intention is to keep all untracked files for a data transfer when I can recover the tracked files by cloning the git repository in the target location.

LeGEC
  • 46,477
  • 5
  • 57
  • 104
chris_f
  • 1,081
  • 8
  • 17
  • 1
    _If_ your files don’t have newlines in their names, then perhaps something like `git ls-files | while read -r file ; do rm "${file}" ; done`? – Biffen Oct 24 '22 at 07:19
  • Could you create a stash including the untracked files and then use this patch? – knittl Oct 24 '22 at 07:19
  • 1
    Your intention is to copy over untracked files ? that would be `git ls-files --exclude-standard -o | xargs tar -cf untracked.tar` (if you want to also copy files that are `.gitignore`d, remove the `--exclude-standard` option). – LeGEC Oct 24 '22 at 07:36
  • 2
    Given that your intent is to *save* the *untracked* files, consider using `git ls-files --other` to get their names (which you can then feed to some archiver to make the saved tarball or whatever). – torek Oct 24 '22 at 07:36
  • 1
    https://stackoverflow.com/a/37781235/7976758 Found in https://stackoverflow.com/search?q=%5Bgit%5D+remove+all+tracked+files – phd Oct 24 '22 at 07:48

0 Answers0