5

git status tells me names of files that are currently untracked. How can I get a dired buffer containing those files? e.g. if I run the command git ls-files --others --exclude-standard, I'd like all of those files in a dired buffer?

Something like M-x find-dired would work, except I don't want the output of find, I need the output of the git command above.

6
  • If you want such feature, why don't you give Project/Helm Projectile a try? It uses Ido or Helm to give you the file list and you can interactive filter the list. With Helm, you can do things like copy/move/delete the files.
    – Tu Do
    Apr 23, 2015 at 10:49
  • Why do you need it? What do you want to do with the files?
    – choroba
    Apr 23, 2015 at 10:53
  • I am not aware of Helm etc yet. Looks like I have a lot to catch up on. Apr 23, 2015 at 10:57
  • 'Why do you need it?' Once I have the buffer list, I'd like to then delete most (but not all) of them after quickly browsing their contents. dired is natural to me for this kind of op. Apr 23, 2015 at 10:58
  • 2
    @StephenEglen Oh I didn't read your requirement carefully. If this is what you want, simply use magit and run magit-status. It will give you a list of untracked files and you can jump on it with RET. EmacsWiki has a short tutorial on it.
    – Tu Do
    Apr 23, 2015 at 14:50

1 Answer 1

5

I think this does what you want:

(defun my/dired-untracked (dir)
  (interactive "DUntracked in directory: ")
  (cd dir)
  (switch-to-buffer (get-buffer-create "*untracked*"))
  (shell-command "git ls-files --others --exclude-standard | xargs ls -l" (current-buffer))
  (dired-mode dir)
  (set (make-local-variable 'dired-subdir-alist)
       (list (cons default-directory (point-min-marker)))))

It prompts for a directory, changes to the directory, runs the git command, and finally turns this into an actual dired buffer.

If you are using magit, however, it is much nicer to see all tracked and untracked files in the buffer created by magit-status. It doesn't behave like a dired buffer but you can visit and delete files from there as well.

2
  • You should cd after you switch buffer, else you mess up the default-directory of the current buffer. Apr 23, 2015 at 16:15
  • Nice ! It could be improved by showing folders instead of flattening them.
    – Ehvince
    Apr 23, 2015 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.