Compare foo.bar from two different branches in MAGIT [duplicate]
Asked Answered
R

3

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?

Remit answered 3/5, 2013 at 12:27 Comment(1)
You can always issue a git command from the magit status buffer by typing : (e.g.: : diff mybranch master -- myfile.cs)Bismuthinite
B
15

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

Bopp answered 3/5, 2013 at 15:59 Comment(3)
Can you write what is the command issued for you with this bind?Remit
Find out for your self. <kbd>C-h k</kbd> <kbd>C-x esc esc</kbd>Bopp
Is there a way to add some (un)folding functionality to the resulting buffer? Like similar to the one available in *magit-diff* buffer.Remit
C
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).

Circum answered 22/11, 2018 at 15:29 Comment(3)
or from the file buffer: C-c M-g D rBismuthinite
=f does not exist...Clinker
The shortcut has changed to --, is that also the case for you?Circum
T
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.

Tetrastichous answered 31/8, 2020 at 19:36 Comment(2)
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.Vallee
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)[masteringemacs.org/article/mastering-key-bindings-emacs] and (Ergoemacs)[ergoemacs.org/emacs/emacs_esoteric.html]. Happy Hacking :)Arrange

© 2022 - 2024 — McMap. All rights reserved.