Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git Commit Parents' Order

Is there some convention for the order of a commit's parents?

Because one of the commit's parents should be to be to previous commit on the current branch that is being merged into and the rest are previous commits of the other merging branches.

I would like to identify the previous commit of the current branch, I'm using pygit which returns a list of parents for a commit and intuitively I thought maybe the order of parents has significance but I have found no explicit mention of this.


I wrote this utility function, using first parent commit to traverse branch :

def walk_branch(pygit_repository, branch_oid):
    """
    Walk a single branch
    """
    from pygit2 import GIT_SORT_TOPOLOGICAL
    previous_first_parent_oid = None
    for commit in pygit_repository.walk(branch_oid, GIT_SORT_TOPOLOGICAL):
        if previous_first_parent_oid is None or commit.oid == previous_first_parent_oid:
            previous_first_parent_oid = commit.parents[0].oid if len(commit.parents) else None
            yield commit
like image 828
Emil Davtyan Avatar asked Aug 18 '13 16:08

Emil Davtyan


People also ask

What is parent 1 and parent 2 in git?

Every commit in git has at least one parent (except for the first/initial commit). A commit's parent is the previous one. C1 is the initial commit. C2 is the second one.

What are parent commits in git?

The parent commit is the commit this current commit is based on. Usually: When you git commit normally, the current commit becomes the parent commit of the new commit that's introduced by the command.

How can a commit have 2 parents?

For example, if a commit has multiple parents, it's a “merge commit” — since it merged multiple commits into one. Or, if a commit has multiple children, it represents the ancestor of a “branch”, etc.

How many parents can a commit have?

A commit object may have any number of parents. With exactly one parent, it is an ordinary commit. Having more than one parent makes the commit a merge between several lines of history. Initial (root) commits have no parents.


1 Answers

libgit2 and its bindings return the parents in the order that they're stored in the commit, which is the order of the commits as they were given on e.g. the command-line for git merge, and it's a constant that the first parent is the current commit when creating a new one (either through merge or normal git commit).

In order to identify the commit previous commit (in time), then all you need to do is look at the first parent. Whether they're in the same branch or not is not something you can see from that, as a commit parent might be in a different branch (which is what causes branches), but you can draw a direct line (like those git log --graph --oneline draws).

like image 197
Carlos Martín Nieto Avatar answered Sep 28 '22 05:09

Carlos Martín Nieto