Thursday, February 10, 2011

Shorthand for diff of git commit with its parent?

Aside from writing an alias or script, is there a shorter command for getting the diff for a particular commit?

git diff 15dc8^..15dc8

If you only give the single commit id git diff 15dc8, it diffs that commit against HEAD.

  • Use git show $COMMIT. It'll show you the log message for the commit, and the diff of that particular commit.

    orip : Too bad it can't use difftool :(
    From mipadi
  • If you know how far back, you can try something like:

    # Current branch vs. parent
    git diff HEAD^ HEAD
    
    # Current branch, diff between commits 2 and 3 times back
    git diff HEAD~3 HEAD~2
    

    Prior commits work something like this:

    # Parent of HEAD
    git show HEAD^1
    
    # Grandparent
    git show HEAD^2
    

    There are a lot of ways you can specify commits:

    # Great grandparent
    git show HEAD~3
    

    See this page for details.

  • Use git show COMMIT to get commit description and diff for a commit. If you want only diff, you can use git diff-tree -p COMMIT

    And if you read git-rev-parse(1) manpage carefully, you would notice the following fragment:

    Two other shorthands for naming a set that is formed by a commit and its parent commits exist. The r1^@ notation means all parents of r1. r1^! includes commit r1 but excludes all of its parents.

    This means that you can use 15dc8^! as a shorthand for 15dc8^..15dc8 anywhere in git where revisions are needed.

    orip : +1 Awesome, `r1^!` works with `git difftool`!!!

0 comments:

Post a Comment