1

The command git log origin/<branchname> does show the commits in the certain branch, but it also shows the commits that were made to the parent branch before the <branchname> was even created.

How to limit the output to contain only those entries that were commited after the branch was created?

exebook
  • 32,014
  • 33
  • 141
  • 226
  • 2
    Those commits are in *both* branches. In Git, many commits are on many branches. Typically there's just one root commit and it is on every branch! The set of branches that contain any given commit changes dynamically as branches are added and deleted. In this process, the *commits themselves* do not change at all. Only the *set of names by which you can find the commits* change. – torek Oct 14 '19 at 04:48
  • @torek so basically the right answer to my question is "you can't"? – exebook Oct 14 '19 at 16:22
  • Sort of. What you need to do is pick *your own* cut-off point. Typically, you know which commits you care about because you want, e.g., "commits reachable from `feature` that are not reachable from `develop`". In this case `develop..feature` works as the argument to `git log` or `git rev-list`. See VonC's answer. – torek Oct 14 '19 at 20:57

2 Answers2

2

Try using git merge-base:

git log $(git merge-base parent-branch branchname)..branchname
# shorter
git log parent-branch..branchname
# which stands for
git log branchname --not parent-branch

That would list all commits after the merge point between the two branches.
This syntax assumes a bash session.

See:

This is linked to git rev-list, where git rev-list foo..bar shows everything on branch bar that isn't also on branch foo.

A special notation "<commit1>..<commit2>" can be used as a short-hand for "^'<commit1>' <commit2>": reachable from commit2, but not from commit1.

See git revisions for more.


Note: Git has no notion of "parent" branch, only a graph of commits: see "How to find the nearest parent of a Git branch?"

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • This will show all commits reachable by `$(git merge-base parent-branch branchname)` and by `branchname` (thus just all commits reachable by `branchname`). You missed the `..` in between. This is *actually* something that is not well documented... – alfunx Oct 14 '19 at 06:16
  • 1
    @alfunx Thank you. I have edited the answer and included the relevant documentation. – VonC Oct 14 '19 at 06:54
  • Oh, what I actually meant was that `man git-log` does not mention what happens if you just list some refs. – alfunx Oct 14 '19 at 07:08
  • @alfunx I agree. I though '..' was the default, but it is better to be explicit in this case. – VonC Oct 14 '19 at 07:09
  • 1
    @alfunx Actually, from https://git-scm.com/docs/gitrevisions#_revision_range_summary: `git log A B` means "all commits reachable from A" *and* "all commits reachable from B" – VonC Oct 14 '19 at 07:24
  • Just a small remark for the original answer: Considering the last quote in your answer, you could just say `parent-branch..branchname` instead of using `merge-base`. – alfunx Oct 14 '19 at 08:08
  • 1
    @alfunx assuming a direct ancestry between the two, yes. I have edited my answer. I mention at the end https://stackoverflow.com/q/3161204/6309, because finding the parent of a branch is not always trivial. – VonC Oct 14 '19 at 08:40
  • Neither worked for me, I always get commits since the initial master commit of README.md, or get zero commits with `..` syntax. – exebook Oct 14 '19 at 16:21
  • maybe because my branches are all merged back into the master branch – exebook Oct 14 '19 at 16:23
  • @exebook Then do a `git branch --oneline --graph --decorate --all --branches`: do you see commits in branchname which are *not* in parent branch? – VonC Oct 14 '19 at 20:07
1

This also works:

git log branch1..branch2
UserASR
  • 2,015
  • 4
  • 24
  • 47