Zach Riggle
Zach Riggle

Reputation: 3085

git diff: Show branch name instead of commit hash

git diff is a great tool for collaboration, but it may be useful sometimes to have the literal values passed into the git diff A B used in the output, instead of the hashes of reference A and B.

Consider the following git diff output:

$ git diff my-first-branch my-second-branch
diff --git a/file b/file
index c77692b..38ea2ea 100644
--- a/file
+++ b/file
@@ -1 +1,2 @@
 Added this file
+Added this line

It would be useful to have the option to display my-first-branch..my-second-branch instead of c77692b..38ea2ea, for example.

Are there any options or settings to do this?

To pre-answer following caveats and answers:

Upvotes: 1

Views: 711

Answers (2)

Zach_is_my_name
Zach_is_my_name

Reputation: 60

For those wanting to this, a script can handle that:

  #!/bin/bash
    
    # prefix git diff with ref names
    ref1=$1
    ref2=$2
    
    if [ -z $ref2 ] 
        then 
             ref1=$(git rev-parse --abbrev-ref HEAD)
             ref2=$1
    fi  
    
    git diff --src-prefix\=${ref1}\-\- --dst-prefix\=${ref2}\-\- $ref1..$ref2

Those unfamiliar with shell scripts can put it txt file named whatever you want to call the command (mine is called gitd). Then make it executable by using the command: chmod +x Next put it in a directory in your PATH variable, I use ~/bin Finally, when you want to run diff with the branch/commit/whatever use gitd <whatever> [whatever]. Note the first <whatever> you chose a branch / commit / whatever. The second inputted [whatever] is optional, causing the first argument to diff to be HEAD, and the second argument to be .

Upvotes: 1

jthill
jthill

Reputation: 60295

Those id's are for the blobs being diffed, whose names are given above (and with text diffs in the output below) them, e.g. a/file is c77692b. The "instead of" you're asking for, with mutable refnames instead of actual blob id's, would be a Very Bad Move.

showcmd () { echo "$@"; "$@"; }

will get you what you want, e.g. showcmd git diff master issue-1234 > myoutput Or

gitdiff() { echo git diff "$@"; git diff "$@"; }

and then gitdiff master issue-1234.

Upvotes: 1

Related Questions