git cherry-pick 使用指南_git

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.