How do you list tracked files (git ls-files) in magit?
Asked Answered
J

6

9

How do you list tracked files (git ls-files) in magit?

Jocundity answered 28/7, 2014 at 11:3 Comment(1)
There exists (in Magit 2.11.0, at least) a function called magit-insert-tracked-files, which can be run using j t... However, that doesn't (at least for me) seem to actually create the section, despite that the code seems to indicate that it would. It instead gives the error Section "Tracked files" wasn’t found... I'm not sure why, or how to fix it, so leaving this as just a comment. Presumably, though, this (or something related to it) would be the way to do this...Butane
D
6

@tarsius is right about the fact that there is currently no built-in command for doing this.

You can, however, do this:

: ls-files RET

: is bound to magit-git-command which allows you to

Execute a Git subcommand asynchronously, displaying the output. With a prefix argument run Git in the root of the current repository. [...]


You can of course automate the process described above by recording a keyboard macro or defining a custom command and binding it to a key sequence of your choice:

(defun magit-ls-files ()
  "List tracked files of current repository."
  (interactive)
  (if (derived-mode-p 'magit-mode)
      (magit-git-command "ls-files" default-directory)
    (message "Not in a Magit buffer.")))

(define-key magit-mode-map (kbd "K") 'magit-ls-files)
Degauss answered 10/8, 2014 at 9:52 Comment(2)
Mode map needs to be modified as a hook (add-hook 'magit-mode-hook (lambda () (define-key magit-mode-map (kbd "K") 'magit-ls-files))) so that magit-mode-map is defined prior to useJocundity
@Jocundity Using lambda to add functionality to hooks has a couple of disadvantages. It makes values of hook variables hard to read, and you can't easily remove functionality via remove-hook. So if you want to use a hook you should wrap the body of your lambda in a defun and add that to the hook instead. Another solution would be to simply (require 'magit) before modifying the mode map or use eval-after-load to wrap the code in my answer.Degauss
J
4

To follow up on the comment from @lindes, you can configure Magit to include a list of tracked files in the status buffer by doing

(magit-add-section-hook
   'magit-status-sections-hook
   'magit-insert-tracked-files
   nil
   'append)

for example in your .emacs file. (See the Status Sections and Section Hooks docs for details on what this does.)

Then, in the Magit status buffer, typing the j t key sequence @lindes mentions will go to the tracked files section.

Jabe answered 15/2, 2019 at 17:59 Comment(0)
X
2

Magit doesn't do that but dired-k shows the (git) status of each file, which might be all you need.

Xenia answered 28/7, 2014 at 20:30 Comment(0)
S
0

The output is not as clean, but what I have always does is l f TAB from the magit-status buffer. That is...

  1. l: lower-case "L" for Logging, which calls magit-key-mode-popup-logging
  2. f: "f" for File log, which calls magit-file-log
  3. TAB: which calls minibuffer-complete

You could make this a little easier with a custom keybinding and/or macro.

Schoenburg answered 1/6, 2015 at 4:21 Comment(0)
D
0

The following snippet works great for me. To use it, run M-x my-git-dired and enter a git work directory (or a subdirectory). The resulting buffer is a normal dired buffer, so it's possible to, for example, query-replace on multiple files.

(defun my-git-dired (dir)
  (interactive
   "DDirectory inside a git repository: \n")
  (condition-case nil
      (dired (cons "*git-dired*" (my-git-ls-files dir)))
    (error (message "Execution of git-ls-files failed"))))

(defun my-git-ls-files (dir)
  (save-excursion
    (cd dir)
    (split-string
     ;; The following is shell-command-to-string with error handling added.
     (with-output-to-string
       (with-current-buffer
           standard-output
         (unless (= 0 (call-process shell-file-name nil t nil
                                    shell-command-switch "git ls-files"))
           (error "Not a git repo")))))))
Downatheel answered 6/10, 2017 at 13:20 Comment(0)
S
0

I just use M-& git ls-files -z | xargs -0 -- ls -ld followed by M-x dired-virtual RET. The latter requires the dired-x library to be loaded.

Steer answered 13/5, 2023 at 7:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.