Git log to get commits only for a specific branch

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

    This will output the commits on the current branch. If any argument is passed, it just outputs the hashes.

    git_show_all_commits_only_on_this_branch

    #!/bin/bash
    function show_help()
    {
      ME=$(basename $0)
      IT=$(cat < default. compares current branch to master
      $ME B1      -> compares branch B1 to master
      $ME B1 B2   -> compares branch B1 to B2
      $ME B1 B2 V -> compares branch B1 to B2, and displays commit messages
      
      )
      echo "$IT"
      exit
    }
    
    if [ "$1" == "help" ]
    then
      show_help
    fi
    
    # Show commit msgs if any arg passed for arg 3
    if [ "$3" ]
    then
      OPT="-v"
    fi
    
    # get branch names
    OLDER_BRANCH=${2:-"master"}
    if [ -z "$1" ]
    then
      NEWER_BRANCH=$(git rev-parse --abbrev-ref HEAD)
    else
      NEWER_BRANCH=$1
    fi
    
    if [ "$NEWER_BRANCH" == "$OLDER_BRANCH" ]
    then
      echo "  Please supply 2 different branches to compare!"
      show_help
    fi
    
    OUT=$(\git cherry $OPT $OLDER_BRANCH $NEWER_BRANCH)
    
    if [ -z "$OUT" ]
    then
      echo "No differences found. The branches $NEWER_BRANCH and $OLDER_BRANCH are in sync."
      exit;
    fi
    
    if [ "$OPT" == "-v" ]
    then
      echo "$OUT"
    else
      echo "$OUT" | awk '{print $2}'
    fi
    

提交回复
热议问题