Search code examples
gitemacsmagit

Viewing changes in context with magit


Consider a magit session started with only the two recommended keybinding adjustments.

(global-set-key (kbd "C-x g") 'magit-status)
(global-set-key (kbd "C-x M-g") 'magit-dispatch)

Now suppose that in this session I am merely perusing a repository, without the change/stage/commit sequence. Now I reach a file.c

// a: This line existed in commit A.
// a: This line existed in commit A.
// b: This is a new line added in commit B.
// b: This is a new line added in commit B.

To understand what has been done at each commit, I go through git checkout incrementally.

When at commit B, I'd like to see in context that the two lines (b above) are newly inserted, perhaps by a highlighted background, or by a margin indicator.

Is viewing changes in context possible with magit, or is git diff the best one can do?

Without magit diff-hl-flydiff can already show how the current file is different from the repo. I'm here asking whether viewing the changes during a series of git checkout is possible, with diff-hl-flydiff if it can coexist nicely with magit, or without if this is built into magit.


Solution

  • There's no need to git checkout anything.

    I suggest using e to invoke magit-ediff-dwim for the commit you are interested in (or Ec to invoke magit-ediff-show-commit). If the commit involves more than one file, it'll prompt you (with completion) for the file to diff. Then it'll jump into ediff where you can peruse the changes for that commit diff in context.

    If you view the log for just one file, using C-cM-gl (or M-x magit-log-buffer-file) then the Magit will assume you want to ediff that same file, and will not prompt you.

    To learn how to use ediff: C-hig (ediff)