git cherry-pick 最佳實踐

引題

最近公司項目,成都小伙伴,在develop上合并了錯誤的代碼。
然后我們自己的分支上也合并了錯誤的代碼。雖然最終develop的代碼回滾了,
錯誤的代碼丟掉,但是自己分支上錯誤的提交還在,創(chuàng)建merge request 的時,發(fā)現(xiàn)一大堆不是自己寫的代碼,如何解決呢?

cherry-pick

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

git-scm#cherry-pick

拙略的英文,翻譯為:可以把已經(jīng)存在提交再次提交。

實戰(zhàn)

總體思路如下:找到自己本工單號,本分支提交的commits,在新分值上merge這些commits。

第一步: 格式化輸出log

git log命令可一接受一個--pretty選項,來確定輸出的格式.
如果我們只想輸出hash.

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

git用各種placeholder 來決定各種顯示內(nèi)容:

  • %H: commit hash
  • %h: 縮短的commit hash
  • %T: tree hash
  • %t: 縮短的 tree hash
  • %P: parent hashes
  • %p: 縮短的 parent hashes
  • %an: 作者名字
  • %aN: mailmap的作者名字 (.mailmap對應(yīng),詳情參照git-shortlog(1)或者git-blame(1))
  • %ae: 作者郵箱
  • %aE: 作者郵箱 (.mailmap對應(yīng),詳情參照git-shortlog(1)或者git-blame(1))
  • %ad: 日期 (--date= 制定的格式)
  • %aD: 日期, RFC2822格式
  • %ar: 日期, 相對格式(1 day ago)
  • %at: 日期, UNIX timestamp
  • %ai: 日期, ISO 8601 格式
  • %cn: 提交者名字
  • %cN: 提交者名字 (.mailmap對應(yīng),詳情參照git-shortlog(1)或者git-blame(1))
  • %ce: 提交者 email
  • %cE: 提交者 email (.mailmap對應(yīng),詳情參照git-shortlog(1)或者git-blame(1))
  • %cd: 提交日期 (--date= 制定的格式)
  • %cD: 提交日期, RFC2822格式
  • %cr: 提交日期, 相對格式(1 day ago)
  • %ct: 提交日期, UNIX timestamp
  • %ci: 提交日期, ISO 8601 格式
  • %d: ref名稱
  • %e: encoding
  • %s: commit信息標(biāo)題
  • %f: sanitized subject line, suitable for a filename
  • %b: commit信息內(nèi)容
  • %N: commit notes
  • %gD: reflog selector, e.g., refs/stash@{1}
  • %gd: shortened reflog selector, e.g., stash@{1}
  • %gs: reflog subject
  • %Cred: 切換到紅色
  • %Cgreen: 切換到綠色
  • %Cblue: 切換到藍(lán)色
  • %Creset: 重設(shè)顏色
  • %C(...): 制定顏色, as described in color.branch.* config option
  • %m: left, right or boundary mark
  • %n: 換行
  • %%: a raw %
  • %x00: print a byte from a hex code
  • %w([[,[,]]]): switch line wrapping, like the -w option of git-shortlog(1).

第二步: 根據(jù)需要輸出自己的commit

根據(jù)上述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.

  • %h : 縮短的commit hash
  • %s : commit信息標(biāo)題
  • %an : 作者名字
  • %aD : 日期, RFC2822格式

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輸出格式

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Add & Commit git init 初始化一個 Git 倉庫(repository),即把當(dāng)前所在目錄變成...
    冬絮閱讀 5,155評論 0 9
  • git常用命令 GIT常用命令備忘:http://stormzhang.com/git/2014/01/27/gi...
    新篇章閱讀 8,887評論 1 26
  • 親愛的自己, 這竟然是自己寫的第60封情書,完全沒想到自己可以堅持這么久,需要Mark一下。盡管與同伴比起來我還是...
    Syneysun閱讀 436評論 0 1
  • 文/熠歆 七夕征文 在簡書上看到“七夕征文”,我好奇的點了進(jìn)去,在簡書快8個月了,遇到很多志趣相投的簡友,唯獨未曾...
    熠歆閱讀 483評論 18 8
  • 歌曲鏈接:http://music.163.com/#/song?id=27698223 洛德(lorde),出生...
    任時光寂靜閱讀 1,733評論 2 3

友情鏈接更多精彩內(nèi)容