Skip to content

Instantly share code, notes, and snippets.

@awood
Created April 22, 2014 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awood/11189030 to your computer and use it in GitHub Desktop.
Save awood/11189030 to your computer and use it in GitHub Desktop.
What changes have been made to a given branch?

Ever wanted to see all the changes that someone has made on a branch and only those changes? Doing a “git diff” won’t help because that will also show the changes that have been made on the branch you’re diffing against. You could use “git merge-base” but that only goes to the last merge. What we need to do is find the oldest common ancestor between the branches and then diff the branch in question against that ancestor. Luckily, you can do this with a git alias.

Add the following to your ~/.gitconfig:

[alias]
    oldest-ancestor = !bash -c 'diff --old-line-format='' --new-line-format='' <(git rev-list --first-parent "${1:-master}") <(git rev-list --first-parent "${2:-HEAD}") | head -1' -
    branchdiff = "!f() { head=${1:-HEAD} ; ancestor=`git oldest-ancestor master $head` ; git diff $ancestor..$head ; }; f"
    visual-branchdiff = "!f() { head=${1:-HEAD} ; ancestor=`git oldest-ancestor master $head` ; git diff --name-only $ancestor..$head | xargs diffuse -s -r $ancestor ; }; f &"

Note that the dashes at the end of the oldest-ancestor alias is important; the dash is so the arguments start with $1 instead of $0.

Now you can just run git branchdiff topic_branch_name and you’ll see all the changes since it branched off of master. If you have diffuse installed, you can run git visual-branchdiff topic_branch_name and a diffuse window with each file diff in its own tab will pop up.

The top two aliases are courtesy of http://stackoverflow.com/questions/1527234 and the last one is my own creation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment