287

What is the best way to get a log of commits on a branch since the time it was branched from the current branch? My solution so far is:

git log $(git merge-base HEAD branch)..branch

The documentation for git-diff indicates that git diff A...B is equivalent to git diff $(git-merge-base A B) B. On the other hand, the documentation for git-rev-parse indicates that r1...r2 is defined as r1 r2 --not $(git merge-base --all r1 r2).

Why are these different? Note that git diff HEAD...branch gives me the diffs I want, but the corresponding git log command gives me more than what I want.

In pictures, suppose this:

         x---y---z---branch
        /
---a---b---c---d---e---HEAD

I would like to get a log containing commits x, y, z.

  • git diff HEAD...branch gives these commits
  • however, git log HEAD...branch gives x, y, z, c, d, e.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • You're using "git log" incorrectly for your purposes from what I can see. I have added my answer below. – Debajit Nov 20 '12 at 02:32

10 Answers10

201

In the context of a revision list, A...B is how git-rev-parse defines it. git-log takes a revision list. git-diff does not take a list of revisions - it takes one or two revisions, and has defined the A...B syntax to mean how it's defined in the git-diff manpage. If git-diff did not explicitly define A...B, then that syntax would be invalid. Note that the git-rev-parse manpage describes A...B in the "Specifying Ranges" section, and everything in that section is only valid in situations where a revision range is valid (i.e. when a revision list is desired).

To get a log containing just x, y, and z, try git log HEAD..branch (two dots, not three). This is identical to git log branch --not HEAD, and means all commits on branch that aren't on HEAD.

Marcin
  • 48,559
  • 18
  • 128
  • 201
Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
  • 33
    Wow, that's confusing. It turns out that using "git diff HEAD..branch" shows all commits (x, y, z, c, d, e), but "git log HEAD..branch" does exactly what I want and only shows x, y, z! This is the exact opposite of using "...". – Greg Hewgill Sep 10 '08 at 07:59
  • 23
    `git diff HEAD..branch` is identical to `git diff HEAD branch`. The key thing to remember here is that log takes a list/range of revisions, while diff doesn't. That's why they treat their args differently. – Lily Ballard Sep 11 '08 at 07:52
  • 4
    Seems that `git diff HEAD...branch` (three dots) corresponds to the output of `git log HEAD..branch` – jchook Oct 31 '18 at 15:20
  • **HOT TIP:** If you want to see all the differences in the current branch since it was created use `git difftool --dir-diff main` (or master). All changes made will be displayed in the defined visual diff and merge tool. – Eduardo Lucio Feb 12 '22 at 22:25
73
git cherry branch [newbranch]

does exactly what you are asking, when you are in the master branch.

I am also very fond of:

git diff --name-status branch [newbranch]

Which isn't exactly what you're asking, but is still very useful in the same context.

towi
  • 21,587
  • 28
  • 106
  • 187
skiphoppy
  • 97,646
  • 72
  • 174
  • 218
  • 8
    'git cherry' outputs a list of commit IDs. Can I convert these into a single diff combining all the changes in every commit? – Jonathan Hartley Jul 20 '11 at 15:44
  • 2
    @JonathanHartley Take the **first** and the **last** like of the commit IDs and thrown them to the `gif-diff`: `git diff x..z`, or for my own example it is `git diff 13bc4d..8eda3a`. – towi May 22 '13 at 09:37
  • 4
    Its hard to understand which code must be replaced in your command as in which one of branch or newbranch is a keyword and which should be replaced with custom branch name – pal4life Jun 16 '14 at 17:50
39

What you want to see is the list of outgoing commits. You can do this using

git log master..branchName 

or

git log master..branchName --oneline

Where I assume that "branchName" was created as a tracking branch of "master".

Similarly, to see the incoming changes you can use:

git log branchName..master
Debajit
  • 46,327
  • 33
  • 91
  • 100
  • 1
    @A-B-B, if branchName is omitted, it defaults to "head", which is effectively branchName in the example above. – Debajit Nov 06 '13 at 01:27
25

This is similar to the answer I posted on: Preview a Git push

Drop these functions into your Bash profile:

  • gbout - git branch outgoing
  • gbin - git branch incoming

You can use this like:

  • If on master: gbin branch1 <-- this will show you what's in branch1 and not in master
  • If on master: gbout branch1 <-- this will show you what's in master that's not in branch 1

This will work with any branch.

function parse_git_branch {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}

function gbin {
    echo branch \($1\) has these commits and \($(parse_git_branch)\) does not
    git log ..$1 --no-merges --format='%h | Author:%an | Date:%ad | %s' --date=local
}

function gbout {
    echo branch \($(parse_git_branch)\) has these commits and \($1\) does not
    git log $1.. --no-merges --format='%h | Author:%an | Date:%ad | %s' --date=local
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Clintm
  • 4,505
  • 3
  • 41
  • 54
25

When already in the branch in question use

git diff master...

Which combines several features:

  • it's super short
  • shows the actual changes
  • Allow for master having moved forward
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
10

Throw a -p in there to see some FILE CHANGES

git log -p master..branch

Make some aliases:

alias gbc="git branch --no-color | sed -e '/^[^\*]/d' -e 's/* \\(.*\\)/\1/'"

alias gbl='git log -p master..\`gbc\`'

See a branch's unique commits:

gbl
Alex V
  • 18,176
  • 5
  • 36
  • 35
7

To see the log of the current branch since branching off master:

git log master...

If you are currently on master, to see the log of a different branch since it branched off master:

git log ...other-branch

NDavis
  • 1,127
  • 2
  • 14
  • 23
4
git log --cherry-mark --oneline from_branch...to_branch

(3dots) but sometimes it shows '+' instead of '='

nopsoft
  • 41
  • 1
2

I found

git diff <branch_with_changes> <branch_to_compare_to>

more useful, since you don't only get the commit messages but the whole diff. If you are already on the branch you want to see the changes of and (for instance) want to see what has changed to the master, you can use:

git diff HEAD master
Dominik Ehrenberg
  • 1,533
  • 1
  • 15
  • 12
0

With Git 2.30 (Q1 2021), "git diff A...B(man)" learned "git diff --merge-base A B(man), which is a longer short-hand to say the same thing.

Thus you can do this using git diff --merge-base <branch> HEAD. This should be equivalent to git diff <branch>...HEAD but without the confusion of having to use range-notation in a diff.

Denton L
  • 15
  • 1
  • 3