19

This answer shows how to see the differences between a specific file foo.bar in two different branches in git. My question is how to do this within magit?

Community
  • 1
  • 1
Dror
  • 12,174
  • 21
  • 90
  • 160
  • 3
    You can always issue a git command from the magit status buffer by typing `:` (e.g.: `: diff mybranch master -- myfile.cs`) – phils May 04 '13 at 02:52

3 Answers3

15

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

event_jr
  • 17,467
  • 4
  • 47
  • 62
7

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).

Dominik Schrempf
  • 827
  • 8
  • 14
2

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.

  • 1
    Could you elaborate on "Select the commits"? In the log, under Recent commits? I always see just the diff for the commit under the pointer. – ericP May 18 '21 at 13:51
  • Just added some more precise informations. ^^ Let me know if this answers your questions, and if I have some more modifications that could be helpful. I'm not quite sure how to add more though without getting out of scope of the question, but more in-depth information about the non-magit emacs specifics can be found at (Mastering Emacs)[https://www.masteringemacs.org/article/mastering-key-bindings-emacs] and (Ergoemacs)[http://ergoemacs.org/emacs/emacs_esoteric.html]. Happy Hacking :) – Raphaël Duchaîne May 27 '21 at 00:53