Search code examples
gitmergegit-log

How do I get the last merge from develop into master in Git?


In order to automate my CI, I need to get the info of the last merge executed from the develop branch into the master branch (or more generically, from a given source branch to a given destination branch). I tried with

git log --oneline --merges  master -20

but this get me a list of all the last 20 merges into master, without differentiating by source branch (leaving me with the cumbersome task to parse and infer the source branch from the comment). Is there a clean and robust way to filter by source branch directly from the command line ?

Note: I do not need for suggestions related to CI or branch-management best practices. We internally use the GitFlow workflow (https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow), so everything repository/CI-related is already in perfect order (methodologically speaking). I just need for a very specific answer to a very specific question, thanks !


Solution

  • Is there a way to filter by source branch directly from the command line?

    No. Branches in Git are just labels. Merges don't remember what branch they were merged to nor from, they only remember their parent commits and commit messages. Once merged both histories are equally part of the branch.

    These are both different views of the same repository.

          < C <-- D <---- I [develop]
         /         \     /
    A < B < E < F < G < H [master]
    
                  <------ I [develop]
                 /       /
    A < B < C < D < G < H [master]
         \         /
          E <--- F
    

    All commits except I are part of the master branch (ie. they can be reached from H). All commits are part of the develop branch (ie. they can be reached from I). Was G a merge from develop or from an unrelated branch? Topologically there is no way to tell.

    Best you can do is hope try to deduce the information from the merge commit message and history. If your merge commit messages are consistent, you might be able to get away with something like this, but it is fragile.

    git log --oneline --merges <destination> | grep "Merge branch '<source>'"