13

For each defect in code I create separate branch. When defect is fixed I merge this branch in master, so I have history like illustrated below (we see two branches with fixes):

          defect1 fix         defect2 fix
         a---b---c---d           e---f
        /             \         /     \
---o---1---x---y---z---2---o---3---w---4---o--- 

The question is how to get diff for fix1 (between branch start (1) and branch end (2)) or fix2 (diff between (3) and (4)) at any point of time (e.g. for any closed defect in past).

Update: actual question is how to figure out SHA summs of a and d or e and f to perform next obvious diff command diff <commit> <commit>

  • Just as a comment. Plastic SCM (www,plasticscm.com) fits perfectly with the branch per task/defect pattern. Using Plastic you can right click a branch and press diff branch. Using the command line you can do `cm diff ` – Daniel Peñalba Jun 06 '12 at 08:19

5 Answers5

9

The answer is simple:

git diff 1..d

This shows the differences between the branching point of your defect1 fix branch (i.e. 1) and it's end (i.e. d).

In order to find the start of the defect1 fix branch, use

git merge-base master defect1-fix-branch

as indicated in this answer: https://stackoverflow.com/a/2458173/520162.
This gives you 1 according to the documentation of git merge-base.

The end of the defect1 fix branch is simply identified by it's name. So, finding all differences introduced in defect1 fix, you need to do

git diff 1..defect1-fix-branch
Community
  • 1
  • 1
eckes
  • 64,417
  • 29
  • 168
  • 201
  • Right, I want changes between `..` and the question is how to figure out `SHAs` of `a` and `d` if they were not tagged. – Alexander Nikolayev Jun 06 '12 at 08:22
  • 1
    Shouldn't it be `git diff ..`? Because you would want to include diff of `1` and `a` also (which is part of development of branch). – Shahbaz Jun 06 '12 at 08:48
  • 1
    `git merge-base master defect1-fix-branch` gave me the sha of `a` and not of `1`. I had to do `git diff a^..defect1-fix-branch`, where `a` is what I got from `git merge-base`. (notice the `^` after `a`) – MiniGod Jun 02 '15 at 15:06
  • 1
    `git merge-base master defect1-fix-branch` gives a, not 1. Not easy to get the oldest ancestor of 2 branches http://stackoverflow.com/questions/1527234/finding-a-branch-point-with-git – Xorax Mar 14 '17 at 15:01
6

Note: this is equivalent, as detailed in "Not able to think of a case where git diff master..lab and git diff master...lab would be different", to:

 git diff master...defect1-fix-branch

git diff A...B is equivalent to git diff $(git merge-base A B) B

git diff dots

(From "git diff doesn't show enough")

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
2

If you want to see what changes a feature branch introduced, after it has been merged, you simply run:

git diff HEAD^..HEAD

On the master branch. That shows differences between the HEAD's (merge commit) first parent and the HEAD, effectively displaying differences the whole feature branch merge brought into the master branch.

No need to make things complex :)

user1338062
  • 11,939
  • 3
  • 73
  • 67
  • If you have multiple commits in your branch, this will only show the diff of the last commit made. – Jake Aug 28 '23 at 19:16
1

What about:

git diff <commit> <commit>

Where the commit parameters are the SHA checksums of the actual commits.

KARASZI István
  • 30,900
  • 8
  • 101
  • 128
0

When the branch has already been merged into master, and the master branch been merged into the branch while working on it, so the commit history get's muddy, you can find the real diff by using the commit from the master branch at the time your branch was last updated, so like this:

git diff "master@{2023-07-31 11:00:00}...mybranch"

So like this if all you want to plug in, is the branch name:

git diff "master@{`git show -s --format=%ci mybranch`}...mybranch"

If you need an easy to use function, then it can be done like this:

gd() {
    date=`git show -s --format=%ci $1`
    base=`git merge-base $1 "master@{$date}"`
    git diff $base $1
}

and use it like:

gd mybranch
Jake
  • 2,106
  • 1
  • 24
  • 23