NovaDev
NovaDev

Reputation: 2961

Use git log to find differences between when originally branched and current

I haven't seen something exactly like this, because most of what is out there talks about showing differences between the current origin/develop and the current branch.

What I'm after is the difference from when I branched off of develop (a month ago, for example; develop has changed since then) and what the changes are in my feature branch.

So, let's say that I branch from develop, change several files, and now I'm wanting to compare the differences between when I originally created the branch and now.

How do I do that?

Upvotes: 1

Views: 63

Answers (2)

torek
torek

Reputation: 489858

TL;DR: git diff develop...feature-X

Background on branches

Branches don't carry creation information—a branch name is just a pointer to one specific commit, with the special feature that when you are "on" the branch (as in, git status says on branch xyz"), and make a new commit, the pointer automatically moves to point to the new commit.

In other words, the branch name automatically points to the tip of the branch. This is how branches work, and in git, we define "a branch" as "every commit we can get to, by starting with the tip commit to which the branch-name points."

(Side note: remote-tracking branches are similar, except that you cannot check them out to get "on" them, so you cannot update the remote-tracking branch. The idea behind a remote-tracking branch is that it keeps track of commits someone else made, in the remote repository, by—over there—getting on that branch and making commits. Since we're only interested in finding commits below, you can use origin/develop anywhere we've used develop.)

That said, as long as you (a) haven't merged anything in from the time you first created the branch, or (b) really want to see what's happened since that point, what you want is to diff against the merge base.

Merge base

Let's draw a bit of the commit graph, to show what I mean. Suppose you're on feature-X and you branched from develop some commits ago, and develop has moved forward since then as well:

... <- o <- * <- o <- o <- o     <-- develop
             \
              o <- o <- o <- o   <-- feature-X

The commit marked * is where the two branches join up: commit *, and every earlier commit, is on both branches, while the commits "after" the one marked * are only on either develop or feature-X. This point where the histories join is the merge base: the commit closest to the tip of the two branches that is on both branches.

It sounds like what you want at this point is to compare the contents of commit * against the contents of the tip-most commit on feature-X.

There are multiple ways to work with the merge base. The most direct is to use git merge-base:

$ git merge-base feature-X develop

This prints the SHA-1 hash for the merge base, and you can now run:

$ git diff <sha-1> feature-X

to compare the given to the commit to which feature-X points (which is, of course, the tip of that branch).

Three-dot syntax

It turns out, though, that the merge base is pretty useful in other ways. Hence there's a special syntax in gitrevisions for working with two branch tips (or any two commits, really) and their merge base, namely the three-dot X...Y syntax.

Normally, develop...feature-X (note the three dots) means "every commit that is on one of these branches—either one—but no commit that is on both branches". That is, it includes all the little o nodes after the merge base we drew as commit *, but not commit * itself, nor any of the commits before it.

With git diff, however, the three-dot syntax has a slightly different meaning:

$ git diff develop...feature-X

This finds the merge base between the two branches—i.e., commit *—and then diffs that merge base against the named right-hand-side commit, i.e., the tip of feature-X.

If you want to see what's happened in develop, simply reverse the two sides:

$ git diff feature-X...develop

As before, git will find the merge base, and then compare it to the right hand side commit, which this time is the tip of develop.

If you forget these details, look at the documentation for gitrevisions and git diff.

Upvotes: 4

nthall
nthall

Reputation: 2915

Two steps here (though it is probably possible to come up with a one-liner if you care to):

1) Find the last commit before the branch was created

2) Take the hash from (1) - let's call it aabbcc - and use it in diff: git diff aabbcc..HEAD

Upvotes: 1

Related Questions