4

I frequently diff against the "master" branch, and would like it to be easier in magit.

Right now, I can do one of these:

  • y, then scroll to master, then hit d d RET.

  • l l, then scroll to the commit, then d r RET

  • d r master RET

  • maybe other ways

What I'd like is something simpler, where I can hit say d m and it would show the diff against master (or even better, against the result of git merge-base master HEAD). How can I achieve that?

1 Answer 1

8

or even better, against the result of git merge-base master HEAD

That would be master...HEAD (see https://stackoverflow.com/questions/7251477/what-are-the-differences-between-double-dot-and-triple-dot-in-git-dif).

Actually what I think I want is the diff between that "common ancestor" commit, and the current working tree (or staging area) rather than the committed HEAD.

For that you can use magit-diff-working-tree (or magit-diff-staged) with magit-git-string to get the merge-base.

(defun magit-diff-master (&optional args)
  "Show diff range master...HEAD"
  (interactive (list (magit-diff-arguments)))
  (magit-diff "master...HEAD" args))

(defun magit-diff-mbase (&optional args)
  "Show diff of $(git merge-base master HEAD) to working tree."
  (interactive (list (magit-diff-arguments)))
  (magit-diff-working-tree
   (magit-git-string "merge-base" "master" "HEAD") args))

(magit-define-popup-action 'magit-diff-popup
  ?m "Diff merge-base master" 'magit-diff-mbase)
7
  • I believe git diff master...HEAD and git diff $(git merge-base master HEAD)..HEAD should be the same? Anyway I'll give this a try and ping here again, thanks for answering. Jul 9, 2015 at 8:43
  • This seems to work; thank you so much! I'll test it for a day or two and mark this as accepted. Thanks for showing me magit-define-popup-action; now I know how to achieve a lot of things. Jul 9, 2015 at 8:50
  • Actually what I think I want is the diff between that "common ancestor" commit, and the current working tree (or staging area) rather than the committed HEAD. It looks like I can achieve this by calling magit-diff with just one commit as argument, where that commit is the output of git merge-base master HEAD. (There may also be other ways.) Might you have any idea how to get this commit inside magit? Jul 9, 2015 at 20:11
  • Yes, you can also use magit-diff-working-tree which makes your intent clearer (of course it just calls magit-diff); see updated answer.
    – npostavs
    Jul 10, 2015 at 5:13
  • I am now using (defun magit-diff-develop (&optional args) "Show diff range develop...HEAD" (interactive) (magit-diff "develop" args)) (global-set-key "\C-cd" 'magit-diff-develop) To show me the diff from our base branch develop to my working tree, which I find useful. We deploy master.
    – nroose
    Aug 20, 2016 at 0:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.