43

I can't seem to get git log --branches to correctly filter its output. It seems as if Git ignores it.

For example, the head of git log --graph --all --decorate, prints:

* commit 3ae0d17538f787bdde68f37f6644ffe9652d8dc1 (HEAD, feature/branch-ignore)
| Author: Chris Lewis <chris@chris.to>
| Date:   Mon Mar 14 17:39:56 2011 -0700
| 
|     Ignore merge commits, as they're going to be duplicating events
|  
* commit 770534e9d77acb03eaf842440c879aec1c5b5500
| Author: Chris Lewis <chris@chris.to>
| Date:   Tue Mar 8 14:39:40 2011 -0800
| 
|     Removed another remote branch check
| 

Let's say I want to filter by master, which should mean these commits are ignored. The head of git log --graph --all --decorate --branches=master, is also:

* commit 3ae0d17538f787bdde68f37f6644ffe9652d8dc1 (HEAD, feature/branch-ignore)
| Author: Chris Lewis <chris@chris.to>
| Date:   Mon Mar 14 17:39:56 2011 -0700
| 
|     Ignore merge commits, as they're going to be duplicating events
|  
* commit 770534e9d77acb03eaf842440c879aec1c5b5500
| Author: Chris Lewis <chris@chris.to>
| Date:   Tue Mar 8 14:39:40 2011 -0800
| 
|     Removed another remote branch check
|  

Git doesn't seem to be filtering. It doesn't seem to make any difference whether --branches is passed with other arguments or not. My Git version is git version 1.7.4.1. Does anyone know how to use this command successfully?

EDIT: All I want to be able to do is get the log of one branch or another, without having to do a checkout first.

cflewis
  • 7,251
  • 7
  • 28
  • 37
  • 2
    Interesting question. None of the comments below so far address what --branches is supposed to do or how it's supposed to work. – Steve Bennett May 15 '12 at 01:52
  • 1
    I have added an answer which now explains why it does work correctly even though it appeared not to be. – Adam Spiers Feb 05 '15 at 00:18

5 Answers5

26

Firstly, (the other) Adam is right that it doesn't make sense to use --all for this: if you only want to see one branch like your question states, why ask for all branches?

Secondly, as already stated in comments to other answers, you don't need --branches; just do git log mybranch.

Thirdly, I can explain why git log --branches=mybranch doesn't work. The git-log(1) man page says:

--branches[=<pattern>]
    Pretend as if all the refs in refs/heads are listed on
    the command line as <commit>. If <pattern> is given, 
    limit branches to ones matching given shell glob. If 
    pattern lacks ?, *, or [, /* at the end is implied.

The last sentence is the crucial point here. If the <pattern> is just mybranch then there is no globbing character, so git-log interprets it as if you'd typed

git log --branches=mybranch/*

which only matches references under $repo/.git/refs/heads/mybranch/*, i.e. branches which begin with mybranch/.

There is a dirty hack to prevent the /* from being assumed:

git log --branches=[m]ybranch

but I can't think of any good reason why you would want to do this rather than just typing

git log mybranch
Adam Spiers
  • 17,397
  • 5
  • 46
  • 65
  • 1
    Then it's only one branch (git log mybranch) or all branches. What if you want, say two branches. --branches, one would think, would be the way to do it. What is? – Emmel Oct 06 '16 at 18:34
  • 3
    @Emmel `git log branch1 branch2` – Adam Spiers Oct 07 '16 at 12:10
  • 1
    I find it amusing that despite being nearly 100k of solid text the official docs for `git log` do not appear to mention that `git log branch` is a thing. https://git-scm.com/docs/git-log – Neutrino May 16 '19 at 15:57
  • @Neutrino, you were looking for [``](https://git-scm.com/docs/git-log#Documentation/git-log.txt-ltrevisionrangegt) section – Phu Ngo Jul 24 '19 at 07:14
  • 1
    " Show only commits in the specified revision range." That section doesn't mention branch at all. – Neutrino Jul 24 '19 at 09:24
15

Because you specified --all, you override any branch specifications you made.

Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141
  • 5
    The lack of a warning seems to be a usability bug to me. – nes1983 Mar 15 '11 at 19:54
  • 2
    As I mentioned in the question, it doesn't seem to make any difference if I use other flags or not. `git log --branches=master` gives the same commits back (formatted differently, obviously). It just isn't working. – cflewis Mar 15 '11 at 21:27
  • 3
    @Lewisham: If you want only master, use `git log master`. – Cascabel Mar 15 '11 at 22:40
9

Let's say your history looked like this

  d -- e [refs/tags/release1]
 /
a -- b -- c [refs/heads/master]
      \
       f -- g [refs/heads/dev1]
        \
         h [refs/heads/dev2]

If you do git log --branches it's the same git log master dev1 dev2, so you'll see commits a,b,c,f,g and h. If you did git log release1 --branches=dev* it's the same as git log release1 dev1 dev2. You'll see a,d,e,b,f,g, and h, but not c.

sholte
  • 1,295
  • 1
  • 9
  • 11
4

Explanation of --branches

git log <commit> lists all commits that are reachable from any <commit> that you list on the command line.

  • --all does the same but pretends that you listed all the refs in refs/.

  • --branches[=<pattern>] does the same but pretends that you listed all the refs in refs/heads. It also allows you to limit with a glob pattern. As a gotcha, if your glob pattern lacks ?, , or [, then an / at the end is implied.

Examples

git log topic1 topic2 topic3

means list all the commits reachable from topic1, topic2, or topic3.

git log -all

means list all the commits that are reachable from any of the refs that are output from git show-ref.

git log --branches="topic*"

means list all the commits that are reachable from from any branch that starts with the prefix topic.

Sources

https://schacon.github.io/git/git-log.html

https://schacon.github.io/git/git-rev-list.html

Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
3

Does anyone know how to use this command successfully?

EDIT: All I want to be able to do is get the log of one branch or another, without having to do a checkout first.

In order to visualise the graph of commits on all branches and remotes do this:

$ git log --graph --branches=* --remotes=* --decorate

Use this with other git-log options to control verbosity, e.g. --oneline, --name-status, etc.

You may have to fetch remote changes first in order to see them. You can fetch all remote changes without applying them to your current branch(es) like this:

$ git fetch --all
Community
  • 1
  • 1
pestrella
  • 9,786
  • 4
  • 39
  • 44