5

I'd like to define a shortcut "git diffn" with this behavior:

git diffn := git diff HEAD HEAD~1
git diffn 1 := git diff HEAD~1 HEAD~2
git diffn 2 := git diff HEAD~2 HEAD~3
...

First one is no problem, but I don't know to to make the rest.

abo-abo
  • 20,038
  • 3
  • 50
  • 71

3 Answers3

8

What about:

git show - shows the last commit

git show HEAD~1 - shows the last but one commit

git show <COMMIT SHA> - shows you any commit

git whatchanged - shows you which files changed against the git log

user1158559
  • 1,954
  • 1
  • 18
  • 23
  • Thanks for these commands. I haven't used them before, but some are useful. But that doesn't answer my question, which was about making a simple shortcut. – abo-abo Jun 24 '13 at 09:07
  • No, this is exactly what you want. To make an alias, put `alias gitdiffn='git show HEAD~$1'` in your .bashrc, but I think you figured out how to make it a git command alias – user1158559 Jun 24 '13 at 12:33
  • while git show HEAD~2 gives me the info that I want, it's not equivalent to what git diff HEAD~2 HEAD~1 does, since I've bound git diff to a script that calls meld when $DISPLAY is available and otherwise a plain diff. – abo-abo Jun 25 '13 at 07:56
  • Ah, sorry, I misunderstood what your command did. You are right! – user1158559 Jun 25 '13 at 10:32
5

Figured it out myself. I've added to ~/.gitconfig this monstrosity:

[alias]
dn = "!sh -c 'if [ $# -eq 0 ] ; then git diff HEAD~1 HEAD ; else git diff HEAD~`expr $1 + 1` HEAD~$1 ; fi' -"

After this,

git dn

works, as well as

git dn 1
git dn 2 
...
abo-abo
  • 20,038
  • 3
  • 50
  • 71
  • BTW: your `HEAD`s are swapped, should be: `git diff HEAD~1 HEAD` etc. I only noticed because I made the same mistake myself :) – 13ren Jun 24 '13 at 09:40
0

Surprisingly (to me), you can combine ~n and ^ syntax, so this is how I was doing it:

a=1; git diff HEAD^~$a HEAD~$a

But there's a simpler way:

a=1; git log -p -1 HEAD~$a;

I find the extra commit info helpfully orienting, but you can customize that away. Note: the manpage notes some minor differences between this and diff's format, which might matter for non-human consumption.

[alias]
diffn = "!sh -c 'git log -p -1 HEAD~$1' -"

Doesn't work with omitted arg, so I guess an if is needed for that.

BTW: found similar discussion here

Community
  • 1
  • 1
13ren
  • 11,887
  • 9
  • 47
  • 64