-1

I'd like to be able to locate and ideally log only the commits for my branch (experiment). In other words, from HEAD of experiment to its starting point. It would be nice if I did not need to know the parent branch.

The reason why

git log HEAD..master

doesn't work for me, is because master is ahead of my branch:

A---B---F---G master
     \
      C---D---E experiment

For the same reason, this post doesn't answer my question.

EDIT:
The reason why I was having trouble is I changed the history on experiment and it diverged from master so git logged all the commits in the branch, since it never found the point that the they shared history. Accepting answer.

young_souvlaki
  • 1,886
  • 4
  • 24
  • 28
  • 1
    https://stackoverflow.com/q/20015808/7976758, https://stackoverflow.com/a/24769534/7976758, https://stackoverflow.com/a/4649377/7976758 Found in https://stackoverflow.com/search?q=%5Bgit-log%5D+branching+point – phd Jul 16 '21 at 22:02
  • Does this answer your question? [Show all commits in a git branch since original branching point from master](https://stackoverflow.com/questions/20015808/show-all-commits-in-a-git-branch-since-original-branching-point-from-master) – SwissCodeMen Jul 16 '21 at 22:47
  • Does this answer your question? [Where did I branch from?](https://stackoverflow.com/questions/9898093/where-did-i-branch-from) – Joe Jul 17 '21 at 01:29
  • The starting point of branch `experiment` is commit `A`. That's the starting point for every branch. (Equivalently, we might as well just say that no branch ever has a starting point: the concept itself makes little sense.) – torek Jul 18 '21 at 05:10

2 Answers2

2

If you branched from master and are now on experiment, then stay on experiment and say

git log master..

Example on my machine. First, actual situation:

% git log --all --oneline --graph
* 5368511 (HEAD -> experiment) z
* 7f0990f y
* 5c7d1be x
| * d0d5a50 (master) c
| * 47a21c4 b
|/  
* 4dcb110 a

And then we say:

% git log --graph --oneline master..
* 5368511 (HEAD -> experiment) z
* 7f0990f y
* 5c7d1be x

But this does not meet the requirement that you don't want to have to work out what branch you branched from. It is actually quite hard to discover that, because Git has no concept of "branched from". We just did a big discussion of that here:

git: diff between current branch and branch creation

You can see it's exactly the same issue: if you know what branch you "branched from", this is easy. If you don't, not so much.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • The problem I'm experiencing is that it `git log` doesn't stop at the start point. It includes commit `A` if we're sticking with my example. And so I have no way of determining which commit is the actual start point. – young_souvlaki Jul 17 '21 at 02:24
  • Well it doesn't include commit A or B in my solution. To me, that's correct behavior. I can't imagine in what sense you'd want commit B to be part of the branch, since it didn't make any difference to anything; it already existed when you started the branch. – matt Jul 17 '21 at 02:50
  • Added actual example from my machine. – matt Jul 17 '21 at 02:56
  • I think you misunderstood me. It was NOT working for me because it DID include commit `A`. We are on the same page with expected behavior. We do not want commit `A` or `B` to be part of the branch. Please see my [EDIT](https://stackoverflow.com/q/68415444/4682839) in the OP that explains why your solution was not working for me at first. – young_souvlaki Jul 20 '21 at 15:58
  • So basically the whole premise of the question was a misrepresentation? – matt Jul 20 '21 at 16:02
  • Ok but then I don't see why my answer is accepted. – matt Jul 20 '21 at 17:20
  • Because that is how you find the starting point, on a normal branch. My finding renders this question a duplicate, because the other posts I and others linked answer it; but, that doesn't change the fact that your answer is correct. I also appreciate the added detail in response to finding the answer without knowledge of the parent branch. – young_souvlaki Jul 21 '21 at 18:16
-2

You are looking for the merge base of master and experiment. As far as Git is concerned, A is no less a part of experiment than it is of master.

$ git merge-base master experiment
B

Once you have that, you can use git log with B^ (to ensure that B itself is included in the output, you want to start with B's parent):

$ git log B^..HEAD
chepner
  • 497,756
  • 71
  • 530
  • 681