113

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?

Community
  • 1
  • 1
eugene
  • 39,839
  • 68
  • 255
  • 489
  • Looks like this answer is part of what you're looking for https://emacs.stackexchange.com/a/7683/16879 – ibizaman Oct 22 '18 at 18:37

6 Answers6

146

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.

Flow
  • 23,572
  • 15
  • 99
  • 156
Ragge
  • 2,079
  • 1
  • 16
  • 8
  • 9
    Since magit 2.1, `magit-file-log` was renamed to `magit-log-buffer-file` – Anton I. Sipos Jul 07 '15 at 23:25
  • 9
    Unfortunately, `magit-log-buffer-file` doesn't restrict the display of each commit to just that file. – Robin Green Aug 21 '15 at 14:35
  • 4
    @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. – wilkystyle Sep 21 '15 at 15:16
  • 5
    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. – Juraj Martinka Apr 29 '17 at 08:52
  • 1
    Unclear what @robin-green and @wilkystyle are describing, but perhaps the keystrokes `l`, `-u`, and `l` will help after `magit-log-buffer-file` – dickmao Sep 27 '18 at 17:04
  • I guess none of the features support log for a file that isn't there any more? – nroose Feb 12 '20 at 17:06
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

Bryan Ash
  • 4,385
  • 3
  • 41
  • 57
  • 1
    I think step 2 is now `--` (`Magit 20200112.2023, Git 2.20.1, Emacs 26.1, gnu/linux`) – Liam Jan 26 '20 at 01:49
  • 1
    Nice, works also with files that were deleted at some point. – Günter Zöchbauer Jul 14 '20 at 06:31
  • 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) – hraban Apr 17 '21 at 17:37
  • @hraban the command `magit-log-buffer-file` is what you want. With Spacemacs you can use ` g f h` – Bryan Ash Apr 18 '21 at 18:33
  • 1
    @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`. – hraban Apr 19 '21 at 08:53
12

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

Bart Vandendriessche
  • 1,372
  • 13
  • 14
  • 2
    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". – r2evans Mar 18 '21 at 13:55
4

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)))
Iain
  • 300
  • 2
  • 13
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
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.

sp3ctum
  • 429
  • 6
  • 9
2

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

hraban
  • 1,819
  • 1
  • 17
  • 27