Compare foo.bar from two different branches in MAGIT

Building up from @dominik-schrempf answer, it is also (now) possible to select the range of the diff with text selection to go quicker than typing the hashes of the commits.

So a quicker (and more visual) sequence could be :

  1. Start from any Magit buffer with a list of commits
    (like any commit log or the recent commits list in the Magit status buffer) :

  2. Select the commits using text selection

    1a. Have the cursor on one end of the wanted commits

    1b. Toggle text selection C-SPC (or V if you use evil-mode)

    1c. Move your cursor over to the other end of the wanted commits

  3. d to open the diff popup

  4. d to open the dwim buffer or r for diff range (They do mostly the same thing in this case, I think)

For the text selection part, it is the set-mark-command function of Emacs.
You can learn more about it by using Emacs' integrated help system:

  • C-h f followed set-mark-command, or;
  • from the execute-extended-command
    (which should be your second best friend after the help prefix key C-h seen above)
    M-x followed by describe-function followed by set-mark-command.

These two paths are equivalent as every Emacs keybindings is calling a function.

In short, use the help prefix and the execute-extended-command often, especially when starting using Emacs.


I use magit to complement vc. vc already has this functionality C-u C-x v =


Just wanted to note that this is now directly possible with magit.

In the Magit status buffer, type d to open up the diff popup.

Then limit the diff to your file of interest with =f and the file name.

Finally, do a diff range r and type in the branch that you want to compare to (one can also type in a specific commit on the same branch, see a similar question).

Tags:

Emacs

Git

Magit