Search code examples
pythongitgitpython

Git: commits by order of merge


I have written a python script that iterates through the last 10 commits in my master branch. Master branch is restricted from commit and can only be merged to.

Consider the below scenario.

Branch 1 has a commit done at 10.00 am with commits 1 & 2 Branch 2 has a commit done at 11.00 am with commits 3 & 4.

I merged branch 2 before branch 1 to master.

Now when I print the commits while iterating it still prints 4 & 3 followed by 2 & 1.

I need to iterate in the order of latest commit merged to master first. Ie. I need it as 2, 1, 4 & 3. Is this possible?


Solution

  • The sort options for git log are:

    • --date-order
    • --author-date-order
    • --topo-order
    • --reverse

    In your case --topo-order will do the job:

    > git log --topo-order --no-merges --format="%ad -- %s"
    2019-03-12 18:05:02 -- c2 (topic1)
    2019-03-12 18:04:59 -- c1 (topic1)
    2019-03-12 18:05:22 -- c4 (topic2)
    2019-03-12 18:05:19 -- c3 (topic2)
    2019-03-12 18:04:47 -- commit on master