magit: how to diff the same file between two different commits on the same branch?
Asked Answered
S

4

17

How can I get the diff between two commits of the same file using magit?

Directly form git one can do this. But I don't know how to do it from within magit. From magit-status it seems that d and D returns the diff of the whole repository. This is very problematic for me as the repository is huge and it causes magit (and in turn emacs) to become unresponsive. Is there a way to ask magit to restrict the search to only one file from the repository?

I also tried, in the magit-status buffer to do : diff $start_commit..$end_commit -- path/to/file. This works, but the output is a simple buffer without any highlighting nor any folding options...

Soke answered 15/5, 2013 at 11:37 Comment(1)
possible duplicate of Compare foo.bar from two different branches in MAGIT (given that branches are just named commits in git, these questions are the same.)Neruda
E
12

In case anyone stumbles across this now, it is now possible to do this in magit (I do this from magit-log but you can probably do it from elsewhere, too):

  1. Type d to open the magit-diff-popup
  2. Type =f to specify the file you want to view (enter path relative to the project root dir)
  3. Type r to do a diff range. You will be asked for the commit range, for example, 4abcde2..HEAD. Alternately, if you highlight the range of commits in magit-log, the range will be set for you automatically with no prompt.

This way, you end up in a magit-diff buffer complete with highlighting and code folding.

Eraste answered 27/3, 2017 at 16:31 Comment(2)
Or with magit-file-mode enabled (probably by way of global-magit-file-mode), you can use C-c M-g D r. You can bind magit-file-popup to something easier to type than C-c M-g if that's too cumbersome.Neruda
M-x magit-diff-popup gets you there too.Doing
S
5

One way to go is to use vc as suggested by this related answer - in particular C-u C-x v =. However, this return a buffer without the possibility to (un)fold chunks.

Soke answered 15/5, 2013 at 11:49 Comment(0)
M
4

You can also do M-x and magit-ediff-compare.

Then enter the SHA of the older commit and then the filename of the file you want to compare to HEAD, all on the same branch.

Monteux answered 14/5, 2021 at 8:15 Comment(0)
J
3

Building up from @cdidyk 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. Select the commits using text selection C-SPC (or V if you use evil-mode)
  2. =f to specify the file (path relative to the project root folder)
  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)
Johny answered 31/8, 2020 at 19:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.