Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out the start commit of a branch?

Tags:

git

From Version Control with Git by Loeliger, 2ed,

Because the original commit from which a branch was started is not explicitly identified, that commit (or its equivalent) can be found algorithmically using the name of the original branch from which the new branch forked:

git merge-base original-branch new-branch
  • Is it correct that in a repository's graph representation, a branch is a path of commits?

  • if looking a graph of a repository, with a branch name pointing to the tip of each branch, but without showing the start commit of each branch, how do you find out the start commit of each branch?

like image 870
Tim Avatar asked Jan 07 '23 22:01

Tim


2 Answers

if looking a graph of a repository, with branch names pointing to some commits as the tips of branches, how do you find out the start commit of each branch?

git log --graph --decorate

// To get the same display as in the image below add the --oneline as well
git log --graph --decorate --oneline

enter image description here

like image 100
CodeWizard Avatar answered Jan 14 '23 22:01

CodeWizard


Is it correct that in a repository's graph representation, a branch is a path of commits?

Maybe. It depends on what you mean by both "branch" and "path" here. (Do you mean the graph-theoretic "path"? If so, probably "no" since "path" just means any non-empty walk through some nodes and edges without doubling back through a node; although now we need to know what you mean by "branch"...)

if looking a graph of a repository, with a branch name pointing to the tip of each branch, but without showing the start commit of each branch, how do you find out the start commit of each branch?

You can't, in general. Here's one fragment of a graph:

            B - C
          /       \
... o - A           F - G    <-- br
          \       /
            D - E

What's the "start commit" of branch br? Was it B, or A, or something earlier than A (in the ... - o chain)? What other branch, if any, did D and/or E come from?

If there are additional names they may provide enough clues to guess:

            B - C
          /       \
... o - A           F - G    <-- br
          \       /
            D - E            <-- newfeature

The log messages may also provide clues; the log message for F might read merge branch newfeature into br for instance. In this case probably D was the "starting commit" for branch newfeature and whoever made commit F made it by doing git merge newfeature while on branch br. The origin of br is therefore probably pre-A.

Reflogs may also provide these kinds of clues.

However, except for the log message, any of these other clues might vanish. Once newfeature is merged in, we can delete the name (and its reflog); and reflog entries expire after 90 days by default (or 30 days if the object to which they point is unreachable from the tipmost object).

On the other hand, that same graph might have come about in a different way:

            B - C
          /       \
... o - A           F - G    <-- br
          \       /
            D - E            <-- origin/br

The start point of origin/br is probably the same as the start point of br, and the merge F happened when someone did a git merge (perhaps as part of a git pull) to combine his/her work with someone else's work. Both users were working on their own br branches, sharing some repository or repositories (perhaps a centralized server).


When you find yourself asking "what was the first commit on branch X", git encourages you to stop asking (or to ask yourself "why do I care?").

like image 41
torek Avatar answered Jan 14 '23 23:01

torek