git cherry-pick 使用指南
git cherry-pick可以選擇某一個分支中的一個或幾個commit(s)來進行操作。例如,假設我們有個穩定版本的分支,叫v2.0,另外還有個開發版本的分支v3.0,我們不能直接把兩個分支合并,這樣會導致穩定版本混亂,但是又想增加一個v3.0中的功能到v2.0中,這里就可以使用cherry-pick了,其實也就是對已經存在的commit 進行再次提交.
簡單用法:
git cherry-pick <commit id>
注意:當執行完 cherry-pick 以后,將會生成一個新的提交;這個新的提交的哈希值和原來的不同,但標識名一樣;
例如:
$ git checkout v2.0分支
$ git cherry-pick 38361a55 # 這個 38361a55 號碼,位于v3.0分支中:
$ git log
commit 38361a55138140827b31b72f8bbfd88b3705d77a
Author: Justin?Justin@xxx.com
Date: Sat Dec 10 00:11:44 2016 +0800
1. 如果順利,就會正常提交。結果:
Finished one cherry-pick.
On branch v2.0分支
Your branch is ahead of 'origin/old_cc' by 3 commits.
2. 如果在cherry-pick 的過程中出現了沖突
Automatic cherry-pick failed.
After resolving the conflicts,mark the corrected paths with 'git add <paths>' or 'git rm <paths>'and commit the result with:
git commit -c 15a2b6c61927e5aed6111de89ad9dafba939a90b
或者:
error: could not apply 0549563... dev
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
就跟普通的沖突一樣,手工解決:
2.1 $ git status # 看哪些文件出現沖突
both modified: app/models/MainActivity.java
2.2 $ vim app/models/MainActivity.java # 手動解決它。
2.3 $ git add app/models/MainActivity.java
2.4 git commit -c <新的commit號碼>
2.5 再次cherry-pick剩余commit
若提示:
error: a cherry-pick or revert is already in progress
hint: try "git cherry-pick (--continue | --quit | --abort)"
fatal: cherry-pick failed
則執行對應操作:
git cherry-pick?--continue
git cherry-pick?--quit
git cherry-pick?--abort
命令集合:
- git cherry-pick <commit id>:單獨合并一個提交
- git cherry-pick -x <commit id>:同上,不同點:保留原提交者信息。
Git從1.7.2版本開始支持批量cherry-pick,就是一次可以cherry-pick一個區間的commit。 - git cherry-pick <start-commit-id>..<end-commit-id>
- git cherry-pick <start-commit-id>^..<end-commit-id>
前者表示把<start-commit-id>到<end-commit-id>之間(左開右閉,不包含start-commit-id)的提交cherry-pick到當前分支;
后者有"^"標志的表示把<start-commit-id>到<end-commit-id>之間(閉區間,包含start-commit-id)的提交cherry-pick到當前分支。
其中,<start-commit-id>到<end-commit-id>只需要commit-id的前6位即可,并且<start-commit-id>在時間上必須早于<end-commit-id>
注:以上合并,需要手動push代碼。
from:?http://www.jianshu.com/p/08c3f1804b36
總結
以上是生活随笔為你收集整理的git cherry-pick 使用指南的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java程序员新手老手都离不开八大开发工
- 下一篇: Git笔记(三)——[cherry-pi