0

Git how to see the first commit of a branch made from master. For example I master branch then commited C1,C2,C3 now created another branch from C3 called myFirstBranch ,

SO now Master commit C1,C2,C3,C4,C5..... myFirstBranch --------------(Made from C3)D1,D2,D3.....

Now from command line I want to see the first commit of myFirstBranch but when i am using git log myFirstBranch | tail -1,

it is displaying master branch first commit which is C1, While i want D1.

Could someone help me on this?

  • But the problem is that that is not what a branch is. C1 really _is_ the earliest commit of myFirstBranch. – matt May 11 '20 at 11:50

2 Answers2

0

Try this solution here: Git - how to find first commit of specific branch

git log master..branch --oneline | tail -1

so you just missed out master..

bitdruid
  • 1
  • 2
  • This I tried already but not working for my scnerio as I have created many branches from the master for example release/1 release/2 release/3 release/4 ...... and so on, now if I am looking history for release/4 it is inclduing the all the release/1/2/3/ and master. but what i am looking for when i did git checkout -b release/4 I don't remember from which branch i created the release/4. – – vaibhav sharma May 12 '20 at 04:47
  • @vaibhavsharma: *... but what i am looking for is [the name or commit I used, even though] I don't remember from which branch i created the release/4*. The thing is, Git literally does not care what you used to create `release/4`. It is *your* responsibility to come up with what names and/or commits to use to limit the output. Git makes *you* remember this. – torek May 12 '20 at 05:54
0

Git how to see the first commit of a branch made from master.

Branches are not made from branches.

There's a bit of a problem with this claim, which is: we haven't really defined the term branch. The word "branch" in Git does not have just one meaning. It has at least two or three. See also What exactly do we mean by "branch"? But whichever meaning we use, branches are not related to other branches the way you would like.

For example I master branch then commited C1,C2,C3 now created another branch from C3 called myFirstBranch ,

(Note that you just said C3 was the last commit on master when you created myFirstBranch.)

SO now Master commit C1,C2,C3,C4,C5..... myFirstBranch --------------(Made from C3)D1,D2,D3.....

Let's draw this, with newer commits towards the right (although Git normally puts newer commits towards the top in git log output):

           C4--C5   <-- master
          /
C1--C2--C3
          \
           D1--D2--D3   <-- myFirstBranch

The name master, in this drawing, means commit C5. The name myFirstBranch means commit D3. However, we often care not just about the last commit, as identified by the branch name, but also all earlier commits as well.

The drawing shows pretty clearly that commits C1 through C3 are "on" (or reachable from, if we want to be more precise) both branches. That's why the command you are using is telling you that C1 is the first commit that is "on" (reachable from) myFirstBranch: because it really is!

The name master can mean either commit C5 only, or commit C5 and all earlier commits (thus reaching back to C1). The name myFirstBranch can, similarly, mean D3 only, or D3 and all earlier commits. Git therefore gives us a way to do set difference. Rather than the more typical math notation of myFirstBranch \ master or myFirstBranch - master, though, it's expressed as master..myFirstBranch or myFirstBranch ^master.

(Both of these do the same thing in git log and other Git commands.)

The drawback here is that this requires you to know which set to subtract away from your own set. Branches—branch names, in Git—do not remember their "parent branch name" either, even though that would provide an automatic way to do this subtraction.

(It's possible to use Git's reflog entries to figure this stuff out semi-automatically, but there is a drawback to trying to do things this way: the reflog entries expire, typically after 90 days, after which it becomes impossible to compute.)

Edit: in a comment, you say:

in the above diagram I made the myFirstBranch branch from C5

This contradicts your earlier claim that you used C3; but let's draw that:

C1--C2--C3--C4--C5   <-- master
                  \
                   D1--D2--D3   <-- myFirstBranch

Again, the commit selected by the name myFirstBranch is D3. As before, D3 has D2 as its parent, and D2 has D1 as its parent. But this time, D1 has C5 as its parent. So this means that all eight commits are on (or contained in) myFirstBranch while only five commits are on / contained-in master.

Still, git log master..myFirstBranch will select commits D1-through-D3 (in the other order, since Git starts at the end—at the commit to which the name points—and works backwards).

torek
  • 448,244
  • 59
  • 642
  • 775
  • Thanks for your answer, in the aove diagram I made the myFirstBranch branch from C5, So now if I am looking the git log for first commit for myFirstBranch , it is displaying C4, while I am looking ofr C5/D1. Any help on this? – vaibhav sharma May 12 '20 at 04:29
  • Suppose `myFirstBranch` initially pointed to `C5` and then you used it to make `D1`, then `D2`, then `D3`. In that case, it now points to `D3`. Commit `D3` itself points back to commit `D2`, which points back to `D1`, which points back to `C5`. The name `master` still points to `C5`. Commit `C5` itself points back to `C4`, which points back to `C3`, and so on. Draw this and you'll see what you have. – torek May 12 '20 at 05:45
  • Use `git log myFirstBranch ^master` as before, and since `master` points to `C5` which points to `C4` and so on, this will exclude `C5` and `C4` and so on, so that you see only `D3`, then `D2`, then `D1`. Spell this `master..myFirstBranch` for convenience. – torek May 12 '20 at 05:46