git cherry-pick. 如何把已經提交的commit, 從一個分支放到另一個分支

來源:互聯網
上載者:User

標籤:blog   http   java   使用   檔案   ar   art   問題   

實際問題: 
  在本地 master 分支上做了一個commit ( 38361a68138140827b31b72f8bbfd88b3705d77a ) , 如何把它放到 本地 old_cc 分支上? 

辦法之一: 使用 cherry-pick.  根據git 文檔:

Apply the changes introduced by some existing commits 

就是對已經存在的commit 進行apply (可以理解為再次提交)

簡單用法:

git cherry-pick <commit id>
 


例如:
$ git checkout old_cc
$ git cherry-pick 38361a68     # 這個 38361a68 號碼,位於:

 
$ git log 
commit 38361a68138140827b31b72f8bbfd88b3705d77a 
Author: Siwei Shen <[email protected]>
Date:   Sat Dec 10 00:09:44 2011 +0800


1. 如果順利,就會正常提交。結果:

Finished one cherry-pick.
# On branch old_cc
# 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 15a2b6c61927e5aed6718de89ad9dafba939a90b


就跟普通的衝突一樣,手工解決:
2.1 $ git status    # 看哪些檔案出現衝突

both modified:      app/models/user.rb 


2.2 $ vim app/models/user.rb  # 手動解決它。 
2.3 $ git add app/models/user.rb
2.4 git commit -c <新的commit號碼>

 

=================================================== 

 

git 的錯誤操作,導致丟失了重要的commit,真是痛不欲生; 
最後通過git神器終於找回了丟失的commit,但是需要總結和反思的地方有一些,同時需要加深git的學習,特獻上本文以供參考 

執行git reset --hard HEAD~1 ,刪除了commit3,但是發現reset錯了,暈菜了…… 
還好有後悔藥(感歎git的強大啊,神馬意外情況都考慮到了)滿血恢複commit3,執行如下步驟: 

Java代碼  
  1. git reflog  
  2. 502dd0f [email protected]{0}: HEAD~1: updating HEAD  
  3. 147b3b5 [email protected]{1}: commit: test3  
  4. 502dd0f [email protected]{2}: commit: test2  
  5. 0692c03 [email protected]{3}: commit (initial): test1  
  6.   
  7. git reset --hard 502dd0f  
  8. git cherry-pick 147b3b5  



丟失的commit3終於回來啦~~~~ 
雖然有利器,但是需要總結和反思的是,慎用reset hard啊,實在不行reset merge也是不錯的選擇。 

 

原文:http://sg552.iteye.com/blog/1300713

參考:http://dmouse.iteye.com/blog/1797267

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.