Git cherry挑选那些包含关键字的提交(跟踪id) [英] Git cherry pick those commits that contain a keyword (tracking id)

查看:263
本文介绍了Git cherry挑选那些包含关键字的提交(跟踪id)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于代码审查目的,我想要


  1. 樱桃挑选特定提交

  2. 创建一个新分支与他们和

  3. 将该分支推送到远程

以便我可以给分支我想创建一个shell脚本并发出简单的命令,比如

  git-review< trackingID> 

输出如下

 分支< currentgitusername> _< trackingID>创建并推送到远程。 

我把上面提到的脚本放在一起。

 #!/ bin / bash 

if [-z $ 1];然后
回声理由:樱桃选择所有提交的主人,匹配的跟踪ID和创建一个新的分支。;
echo;
echoUsage:$ 0 traackingID;
echo;
出口1;
fi

#如果$ 1与AGW-< number>不匹配模式,抛出一个错误
#如果你没有在提交中添加追踪ID,祝你好运。

user =$(id -u -n)> / dev / null

echoYou are - $ user

branchname = $ user_$ 1_review

echoCreating分支 - $ branchname

git checkout -b $ branchname> / dev / null

git log master --pretty =%H --grep =$ 1|同时阅读rev
do
echo $ rev
#git cherry-pick $ rev> / dev / null
完成

#git push&> / dev / null

echo创建分支,挑选樱桃,推送到远程,现在切换回主控,再见。

git checkout master

但是陷入了两个地方。


  1. 我想验证trackingID格式。它应该是 AGW-< somenumber>

  2. 看起来樱桃挑选合并问题。

      myname @ mymachine〜/ myproj 
    $ ../git-review.sh AGW-1234
    您是 - myname
    创建分支 - myname_AGW-1234_review
    切换到新分支'myname_AGW-1234_review'
    2dfafa89e10062e7cfccb1d7a947ebb848c40cc6
    以前的樱桃选项现在为空,可能是由于冲突解决。
    如果你想提交它,请使用:

    git commit --allow-empty

    否则,请使用'git reset'
    1e295000bc3d80880518c9cac5e34ef3b28fc29e
    错误:对以下文件的本地更改将被合并覆盖:
    rest-service / src / main / java / package / HealthCheckDAOImpl.java
    请提交您的更改或隐藏它们在你可以合并之前。


我是否以错误的方式挑选樱桃?

解决方案

正如在@novelocrat的评论中指出的那样,你是樱桃挑错顺序。默认情况下,git log将从最新的提交输出到第一次提交。为此,请将 git log --oneline 的输出提供给 tac ,或使用 - reverse flag:

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

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






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

当你这样做时,当前分支的所有历史记录都是从中挑选出来的。



需要创建一个孤儿的git分支,像这里提到的


For code review purpose, I want to

  1. cherry pick specific commits
  2. create a new branch with them and
  3. push that branch to remote

so that I can give branch url to peers for review.

I want to create a shell script and issue simple command like

git-review <trackingID>

It gives output as below

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

I put together a script that does above mentioned steps.

#!/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

But got stuck in couple of places.

  1. I want to validate the trackingID format. It should be AGW-<somenumber>
  2. Looks like the cherry-picks have merge issues.

    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.
    

Am I cherry picking in wrong way ? Also, please suggest any changes to make this script robust.

解决方案

As already pointed in the comments by @novelocrat, You are cherry picking in the wrong order. By default, git log will output from the latest commit to the first commit. To do this, either feed in the output of git log --oneline to tac, or use the --reverse flag:

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

The second option is obviously prefered, because that ensures line by line output, while the first (using tac) will need to be fed all the input in one go.


Also, since you are creating a new branch out of master, when you do git checkout -b $branchname.

When you do that, all the history of the current branch is picked from that.

So you need to create an orphaned git branch, like mentioned here.

这篇关于Git cherry挑选那些包含关键字的提交(跟踪id)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆