Search code examples
gittortoisegitgit-log

How to list only all merges to a branch after a specific commit/datetime?


I have multiple branches (dev, release_1, release_2, etc.). A lot of merges occur. I want to see all the merges to a branch after a specific commit / datetime.

For example, in dev branch there is a commit 9de0a508. I want to find all the merges to dev branch after commit 9de0a508. How can I do that using the command prompt or TortoiseGit?

I also need to know the source branch of the merge. For example Merge#1 occurred from release_5 branch while Merge#2 occurred from release_1 branch.


Solution

  • Command line answer

    The option you're looking for is --merges (doc).

    Merges since a specific commit:

    git log --merges 9de0a508..dev
    

    Merges after a given date: (doc)

    git log --merges --after=<your date here> dev
    # or (simple syntax alternative, same effect)
    git log --merges --since=<your date here> dev
    

    Edit: It seems now that you want to know the name of the branch which has been merged in for each merge, and I'm afraid the answer will be slightly less satisfying to read.

    In git merges don't happen between branches, but between commits. When you ask git to merge branch A into branch B, it starts by translating your convenient human-readable placeholders (branch names) to the "real thing", commits.

    When you look into a merge commit (namely, with git cat-file -p <hash>) you can notice that parents are just commits.

    $ git cat-file -p HEAD
    tree abf74cd1fbbdbf39a0c21ee08cff1d977b90b6ed
    parent fc03c55eb0d45d056074be0d22662a8c5fe003e4
    parent 5c48b224800ad812fff36baacf0235bcc531029c
    author Joe Schmoe <[email protected]> 1723450146 +0000
    committer Joe Schmoe <[email protected]> 1723450146 +0000
    
    Merged PR 645342: Some important fixes
    

    The consequence is that nothing is written anywhere about which branches might point at these commits, and more annoyingly, have pointed to it at the time of the commit. Because branches come and go, they can move or be deleted.

    (Note: You might also have merged commits directly, we don't need any branch to actually merge commits.)

    So what can you do? You can, however, retrieve the parent hash you need (the second parent, since the first parent is on the receiving branch, meaning your dev branch here). For this, use --pretty="%P" to extract both parents from the commit and use something like cut or sed to keep only the second part. Then, you'll be able to try git branch -a --points-at=<hash of the parent>, but even then the answer is fragile. You can have multiple branches pointing at it, the original branch can have moved or have been deleted...

    Looking for this information in the Pull Requests history like TTT hinted at in comments seems a more pragmatic way to achieve this, imho, but it's only possible if all merges are done through PRs...