引題
最近公司項目腰素,成都小伙伴蛤织,在develop
上合并了錯誤的代碼。
然后我們自己的分支上也合并了錯誤的代碼不翩。雖然最終develop
的代碼回滾了兵扬,
錯誤的代碼丟掉,但是自己分支上錯誤的提交還在口蝠,創(chuàng)建merge request
的時器钟,發(fā)現(xiàn)一大堆不是自己寫的代碼,如何解決呢妙蔗?
cherry-pick
git-cherry-pick - Apply the changes introduced by some existing commits
拙略的英文俱箱,翻譯為:可以把已經(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!