Pages

Monday 4 August 2014

Differences between git branches

The problem

Given I have two branches, A and B within a git repository, I would like to come up with two sets of changes: (A-B) and (B-A). So I would like to find all the commits that are in A but not in B, and vice versa.

Introducing the same change to two branches

Let's set up a git repository to play with. As a first step, I create an initial commit, with only one file in it, a simple greeting:
$ git init
$ echo "hello there" > hello.txt
$ git add hello.txt
$ git commit -m "Initial commit"
And now, make the branching, and introduce the same change to each of those branches:
$ git checkout -b A
$ echo "blah" > blah.txt
$ git add blah.txt
$ git commit -m "Added blah"
And see the B branch:
$ git checkout master -b B
$ echo "blah" > blah.txt
$ git add blah.txt
$ git commit -m "Added blah"
See if git is smart enough to find out that the two changes are the same:
$ git log A..B --oneline | wc -l
1
Of course, they are not the same, they are copies of the other. So let's see the other command, called git-cherry:
$ git cherry A B
This shows only one line, with a minus sign. From the documentation of git cherry:
git cherry [-v] [<upstream> [<head> [<limit>]]]

       Every commit that doesn’t exist in the <upstream> branch has its id (sha1) reported,
       prefixed by a symbol. The ones that have equivalent change already in the <upstream>
       branch are prefixed with a minus (-) sign, and those that only exist in the <head>
       branch are prefixed with a plus (+) symbol: