Git log to get commits only for a specific branch

后端 未结 12 1701
挽巷
挽巷 2020-11-29 15:13

I want to list all commits that are only part of a specific branch.

With the following, it lists all the commits from the branch, but also from the parent (master)

12条回答
  •  野性不改
    2020-11-29 15:21

    Fast answer:

    git log $(git merge-base master b2)..HEAD
    

    Let's say:

    1. That you have a master branch

    2. Do a few commits

    3. You created a branch named b2

    4. Do git log -n1; the commit Id is the merge base between b2 and master

    5. Do a few commits in b2

    6. git log will show your log history of b2 and master

    7. Use commit range, if you aren't familiar with the concept, I invite you to google it or stack overflow-it,

      For your actual context, you can do for example

      git log commitID_FOO..comitID_BAR
      

      The ".." is the range operator for the log command.

      That mean, in a simple form, give me all logs more recent than commitID_FOO...

    8. Look at point #4, the merge base

      So: git log COMMITID_mergeBASE..HEAD will show you the difference

    9. Git can retrieve the merge base for you like this

      git merge-base b2 master
      
    10. Finally you can do:

      git log $(git merge-base master b2)..HEAD
      

提交回复
热议问题