View a file's history in Magit?
Asked Answered
T

6

115

View the change history of a file using Git versioning talks about other ways of viewing history of a file in Git.

Can it be done in Emacs Magit?

Torras answered 16/1, 2013 at 7:1 Comment(1)
Looks like this answer is part of what you're looking for emacs.stackexchange.com/a/7683/16879Langobard
T
148

Since magit 2.1: magit-log-buffer-file (as per comment below)

Before magit 2.1: magit-file-log is what you are looking for. It will show you all commits for the file in the current buffer in the standard magit log view.

Transcalent answered 12/4, 2013 at 8:30 Comment(6)
Since magit 2.1, magit-file-log was renamed to magit-log-buffer-fileUttasta
Unfortunately, magit-log-buffer-file doesn't restrict the display of each commit to just that file.Melosa
@CatalinHritcu It works well at displaying all of the commits that affect that file, but it doesn't restrict the display of each commit to just that file.Leesaleese
The most convenient way I've found so far is to invoke magit-log-buffer-file and then run magit-ediff-dwim (bound to "e" shortcut in Spacemacs) on the commit of your choice. This will show a window with two panes with highlighted changes.Brickwork
Unclear what @robin-green and @Leesaleese are describing, but perhaps the keystrokes l, -u, and l will help after magit-log-buffer-fileDownstroke
I guess none of the features support log for a file that isn't there any more?Guam
A
45

Open your magit-status buffer, by typing M-x magit-status (I used to have this bound to C-. C-g because it is used all the time. These days, I use Spacemacs so it's <SPC> g s)

  1. Type l to get log viewing option
  2. Type -- to set the "Limit to files" option (used to be =f)
  3. Enter the file path you wish to view history for
  4. Type l to view the log of the current branch

If you're using Spacemacs, you can get the history of the currently visited file using <SPC> g f h

Acerbate answered 30/5, 2016 at 17:2 Comment(5)
I think step 2 is now -- (Magit 20200112.2023, Git 2.20.1, Emacs 26.1, gnu/linux)Singlehearted
Nice, works also with files that were deleted at some point.Tomekatomes
is there an easy way to automatically enter the filename associated with the buffer you were visiting before you entered magit status? (in step 3)Kieffer
@Kieffer the command magit-log-buffer-file is what you want. With Spacemacs you can use <SPC> g f hAcerbate
@BryanAsh unfortunately I don't use Spacemacs. I added my own entry for it in the magit log transient: (transient-append-suffix 'magit-log "l" '("f" "Current file" magit-log-buffer-file)), access with C-x M-g l f.Kieffer
V
12

In your *magit: <project>* buffer use l to go into logging mode, then press f to be prompted for a filename.

Valise answered 8/1, 2014 at 14:25 Comment(1)
This is no longer an option in the transient map, magit-20210303.1208 ... not sure when it changed, perhaps related to @Ragge's mention of "before/since magit-2.1".Curvilinear
G
5

If magit (user manual) doesn't have that feature, then you can have a look at other Emacs mode, and add you own git-log-file function:

(defun git-log-file ()
  "Display a log of changes to the marked file(s)."
  (interactive)
  (let* ((files (git-marked-files))
         (buffer (apply #'git-run-command-buffer "*git-log*" "git-rev-list" \
"--pretty" "HEAD" "--" (git-get-filenames files))))  (with-current-buffer buffer
      ; (git-log-mode)  FIXME: implement log mode
      (goto-char (point-min))
      (setq buffer-read-only t))
    (display-buffer buffer)))
Gooding answered 16/1, 2013 at 7:14 Comment(3)
Or just use C-x v lShack
I am having following message: Symbol’s function definition is void: git-marked-filesDibble
@alp 7 years later, that issue would be better represented in its own question, with the exact version of the OS, eclipse and magic plugin.Gooding
H
4

I do not know a way. I simply use M-x vc-print-log which seems to accomplish the same feat. It is not a magit-integrated way, though.

Hornet answered 12/4, 2013 at 5:16 Comment(0)
K
3

Hit C-cM-g to bring up the magit-file-dispatch: it has several "file-local" magit commands, such as blame and log (l).

Kieffer answered 4/4, 2022 at 4:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.