7

How can I open a git diff in sublime from the terminal?

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
tnrich
  • 8,006
  • 8
  • 38
  • 59

2 Answers2

4

First, make sure you have the subl command accessible from the command line: Open Sublime Text from Terminal in macOS

Next, in your .profile or .bashrc or wherever you keep your aliases, add:

#open diff in sublime. 
#ex: gd   
#ex: gd head^   
#ex: gd 7b3f441147f7c3c4b27bb7c9658aef27e3d0a5eb ee49bbc57f7376bc9f5c951e13808cb6b66be3d8
gd() {
    if [ $# -eq 0 ]
        then
            git diff | subl
        else
            git diff $@ | subl
    fi
}

And you can now open your diffs in sublime right from your terminal.

Community
  • 1
  • 1
tnrich
  • 8,006
  • 8
  • 38
  • 59
  • This has worked great for years and appears to have regressed in Build 4107. Searching for any updates. – johnny Jun 04 '21 at 21:32
1

If you're looking to do this as a one-off, and if you are comparing a and b with subl as an alias for Sublime, you can write git diff a b | subl as per the body of the above