Git log to get commits only for a specific branch

后端 未结 12 1635
挽巷
挽巷 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:39

    I found this approach relatively easy.

    Checkout to the branch and than

    1. Run

      git rev-list --simplify-by-decoration -2 HEAD
      

    This will provide just two SHAs:

    1) last commit of the branch [C1]

    2) and commit parent to the first commit of the branch [C2]

    1. Now run

      git log --decorate --pretty=oneline --reverse --name-status <C2>..<C1>
      

    Here C1 and C2 are two strings you will get when you run first command. Put these values without <> in second command.

    This will give list of history of file changing within the branch.

    0 讨论(0)
  • 2020-11-29 15:42

    You could try something like this:

    #!/bin/bash
    
    all_but()
    {
        target="$(git rev-parse $1)"
        echo "$target --not"
        git for-each-ref --shell --format="ref=%(refname)" refs/heads | \
        while read entry
        do
            eval "$entry"
    
            test "$ref" != "$target" && echo "$ref"
        done
    }
    
    git log $(all_but $1)
    

    Or, borrowing from the recipe in the Git User's Manual:

    #!/bin/bash
    git log $1 --not $( git show-ref --heads | cut -d' ' -f2 | grep -v "^$1" )
    
    0 讨论(0)
  • 2020-11-29 15:43

    From what it sounds like you should be using cherry:

    git cherry -v develop mybranch
    

    This would show all of the commits which are contained within mybranch, but NOT in develop. If you leave off the last option (mybranch), it will compare the current branch instead.

    As VonC pointed out, you are ALWAYS comparing your branch to another branch, so know your branches and then choose which one to compare to.

    0 讨论(0)
  • 2020-11-29 15:43

    BUT I would like to avoid the need of knowing the other branches names.

    I don't think this is possible: a branch in Git is always based on another one or at least on another commit, as explained in "git diff doesn't show enough":

    enter image description here

    You need a reference point for your log to show the right commits.

    As mentioned in "GIT - Where did I branch from?":

    branches are simply pointers to certain commits in a DAG

    So even if git log master..mybranch is one answer, it would still show too many commits, if mybranch is based on myotherbranch, itself based on master.

    In order to find that reference (the origin of your branch), you can only parse commits and see in which branch they are, as seen in:

    • "Git: Finding what branch a commit came from".
    • "How can I see what branch another branch was forked from?"
    0 讨论(0)
  • 2020-11-29 15:43

    I'm using the following commands:

    git shortlog --no-merges --graph --abbrev-commit master..<mybranch>
    

    or

    git log --no-merges --graph --oneline --decorate master..<mybranch>
    
    0 讨论(0)
  • 2020-11-29 15:44

    I needed to export log in one line for a specific branch.

    So I probably came out with a simpler solution.
    When doing git log --pretty=oneline --graph we can see that all commit not done in the current branch are lines starting with |

    So a simple grep -v do the job:
    git log --pretty=oneline --graph | grep -v "^|"

    Of course you can change the pretty parameter if you need other info, as soon as you keep it in one line.

    You probably want to remove merge commit too.
    As the message start with "Merge branch", pipe another grep -v and you're done.

    In my specific ase, the final command was:
    git log --pretty="%ad : %an, %s" --graph | grep -v "^|" | grep -v "Merge branch"

    0 讨论(0)
提交回复
热议问题