Git cherry挑选那些包含关键字的提交(跟踪ID)

时间:2015-02-26 06:33:24

标签: git shell

出于代码审查的目的,我想

  1. cherry pick specific commits
  2. 使用它们创建一个新分支
  3. 将该分支推送到远程
  4. 这样我就可以将分支网址提供给同行进行审核。

    我想创建一个shell脚本并发出简单的命令,如

    git-review <trackingID>
    

    它提供如下输出

    Branch <currentgitusername>_<trackingID> created and pushed to remote.
    

    我整理了一个执行上述步骤的脚本。

    #!/bin/bash
    
    if [ -z $1 ]; then
        echo "Rationale: Cherry pick all commits in master, matching the tracking ID and create a new branch.";
        echo "";
        echo "Usage:  $0 traackingID";
        echo "";
        exit 1;
    fi
    
    #If $1 doesn't match a AGW-<number> pattern, thrown an error
    #Best of luck if you did not add add tracking ID in commits.
    
    user="$(id -u -n)" > /dev/null
    
    echo "You are - $user"
    
    branchname=$user"_"$1"_review"
    
    echo "Creating branch - $branchname"
    
    git checkout -b $branchname > /dev/null
    
    git log master --pretty=%H --grep="$1" | while read rev
    do
      echo $rev
     # git cherry-pick $rev > /dev/null
    done
    
    #git push &> /dev/null
    
    echo "Created branch, cherry picked, pushed to remote. Now switching back to master. Bye."
    
    git checkout master
    

    但是陷入了几个地方。

    1. 我想验证trackingID格式。它应该是AGW-<somenumber>
    2. 看起来樱桃选择有合并问题。

      myname@mymachine ~/myproj
      $ ../git-review.sh AGW-1234
      You are - myname
      Creating branch - myname_AGW-1234_review
      Switched to a new branch 'myname_AGW-1234_review'
      2dfafa89e10062e7cfccb1d7a947ebb848c40cc6
      The previous cherry-pick is now empty, possibly due to conflict resolution.
      If you wish to commit it anyway, use:
      
          git commit --allow-empty
      
      Otherwise, please use 'git reset'
      1e295000bc3d80880518c9cac5e34ef3b28fc29e
      error: Your local changes to the following files would be overwritten by merge:
              rest-service/src/main/java/package/HealthCheckDAOImpl.java
      Please, commit your changes or stash them before you can merge.
      
    3. 我是以错误的方式采摘樱桃吗?此外,请建议任何更改,以使此脚本健壮。

1 个答案:

答案 0 :(得分:1)

正如@novelocrat在评论中指出的那样,你选择了错误的顺序。默认情况下,git log将从最新提交输出到第一次提交。要执行此操作,请将git log --oneline的输出提供给tac,或使用--reverse标记:

git log --oneline | tac
git log --oneline --reverse

第二个选项显然是首选,因为它可以确保逐行输出,而第一个选项(使用tac)将需要一次性输入所有输入。


此外,由于您正在创建一个新的分支,当您执行git checkout -b $branchname

当您这样做时,将从中挑选当前分支的所有历史记录。

所以你需要创建一个孤立的git分支like mentioned here