How do you list tracked files (git ls-files) in magit?
@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)
(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 use –
Jocundity 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 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.
Magit doesn't do that but dired-k shows the (git) status of each file, which might be all you need.
The output is not as clean, but what I have always does is l f TAB from the magit-status
buffer. That is...
- l: lower-case "L" for
Logging
, which callsmagit-key-mode-popup-logging
- f: "f" for
File log
, which callsmagit-file-log
- TAB: which calls
minibuffer-complete
You could make this a little easier with a custom keybinding and/or macro.
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")))))))
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.
© 2022 - 2024 — McMap. All rights reserved.
magit-insert-tracked-files
, which can be run usingj 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 errorSection "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