sensorario
sensorario

Reputation: 21698

See all commits from the begin of a branch (see only current branch)

Exists a way to "git log" all commit of current branch from here to the moment we'd created this branch? A sort of "$ git log --current-branch ..."

* b137094 Alter this stuff
* b2046b6 Change that stuff
* e6fd3b8 Hello world
* 31f3922 Lol
* d44552a ROTFL
*   7050d07 Hey you the rock steady crew
|\ 
| *   d8a9fcf Queens
| |\
* | | c1d6a37 Bluvertigo
* | | 83a7565 Luciano Ligabue
* | |   cfe48b0 Vasco Rossi
|\ \ \
. . . .
. . . .

Watching this graph, what I mean is the same result of

$ git log --oneline --graph 7050d07..HEAD

Upvotes: 0

Views: 60

Answers (1)

torek
torek

Reputation: 489818

(Note that git log 7050d07..HEAD "means" git log HEAD ^7050d07 where the prefix ^ is like a short-term --not. This may not make sense yet; see below, and study the documentation for gitrevisions and git rev-list. This is key to understanding the two git aliases below.)

Git does not have a notion of "when a branch was created". A branch name is simply a label that moves; and more specifically, a local branch is a name moves automatically as new commits are added while on that branch. So you can't quite get what you asked for, but you might be able to get what you want.

Git does have what I refer to as a "branch structure": a piece of the commit graph. It looks to me like what you want here is a git log that stops when the branch-structure has a fork, i.e., stops just before the first commit that has two parents. But it's not clear whether this is really what you want. For instance, consider this graph:

               *
              / \
... o--o--*--*   *--*   <-- br1
        \     \ /
         \     *
          \
           o--o   <-- br2

where br1 and br2 are actual branch labels. There's a fork-and-join in branch br1 and you might want your git log to show all (but only) the *-ed commits. On the other hand, maybe you want only the single end * commit.

I have an alias that does the former, using the branch labels and git for-each-ref:

[alias]
    ltf = !git log HEAD --not $(git for-each-ref \
            --format='%(refname:short)' refs/heads/ | \
            grep -v "^$(git symbolic-ref -q --short HEAD)$") --not

(The second --not is usually unnecessary: it reverses the effect of the first --not so that, e.g., a tag-name argument can add more commits, instead of subtracting commits. Also, the name ltf is from a vague idea I had and is not very meaningful, but it's what's still in my ~/.gitconfig...)

The idea here is to use for-each-ref to produce all local branch names (in short form), discard the current branch if we're on one (grep -v with the name from git symbolic-ref) , and use those branch names to exclude any commits on other branches.

(Any additional arguments are tacked on to the alias as usual, so git ltf --oneline --graph works here.)

If you really do want to exclude all commits starting from the first merge (two-or-more-parent) commit and working backwards, the trick here to use git rev-list with --merges or --min-parents=2 (these are the same selectors) to locate it:

git rev-list --merges -1 HEAD

That's the commit-ID to supply with --not (or a prefix ^, but if there is no such rev this goes awry, so --not is superior here):

[alias]
    ltm = !git log HEAD --not $(git rev-list --merges -1 HEAD) --not

(Again, the second --not reverses the effect of the first --not, just as in the ltf alias above.) So with this alias, git ltm --graph --oneline would produce:

* b137094 Alter this stuff
* b2046b6 Change that stuff
* e6fd3b8 Hello world
* 31f3922 Lol
* d44552a ROTFL

(The --graph here is guaranteed to be boring since there are no forks.)

Upvotes: 2

Related Questions