干货git study要吃饭

git cherry-pick 最佳实践

2015-05-21  本文已影响1213人  AQ王浩

引题

最近公司项目,成都小伙伴,在develop上合并了错误的代码。
然后我们自己的分支上也合并了错误的代码。虽然最终develop的代码回滚了,
错误的代码丢掉,但是自己分支上错误的提交还在,创建merge request 的时,发现一大堆不是自己写的代码,如何解决呢?

cherry-pick

git-cherry-pick - Apply the changes introduced by some existing commits

git-scm#cherry-pick

拙略的英文,翻译为:可以把已经存在提交再次提交。

实战

总体思路如下:找到自己本工单号,本分支提交的commits,在新分值上merge这些commits

第一步: 格式化输出log

git log命令可一接受一个--pretty选项,来确定输出的格式.
如果我们只想输出hash.

   git log --pretty=format:"%h" 

git用各种placeholder 来决定各种显示内容:

第二步: 根据需要输出自己的commit

根据上述format,定制

git log -150 --pretty=format:"%h %s %an %aD " | grep wanghao | grep xxxxx

其中 -150 表示输出150次的commits

-<1number>, -n <1number>, --max-count=<1number>
Limit the number of commits to output.

grep wanghao grep xxxxx 管道符输出

是 wanghao 并且是工单 xxxxx 的commits

最终输出如下:

98743af [rmxxxxx] Merge branch 'develop' xxxxx
2b48938 [rmxxxxx] rmxxxxxx wanghao 
3320829 [rmxxxxx] rmxxxxxx ****** wanghao Mon, 18 May 2015 14:42:51 +0800
1e09778 [rmxxxxx] rmxxxxxx ****** wanghao Sun, 17 May 2015 18:07:44 +0800

第三步: 运用强大的cherry-pick

    git checkout develop
    git pull origin develop
    git checkout -b feature/rmxxxxx_wanghao_20150521_xxxx
    git cherry-pick 2b48938 3320829 1e09778
    git branch -D feature/rmxxxxx_wanghao_20150521_xxxx_tmp
    
    git push origin :feature/rmxxxxx_wanghao_20150521_xxxx
    git push origin feature/rmxxxxx_wanghao_20150521_xxxx

bingo!

参考:
ruby-china#个性化你的Git log输出格式

上一篇 下一篇

猜你喜欢

热点阅读