這段時間做項目間的代碼分離,從git上的A庫 fork一份到B庫,然后A庫和B庫就各自獨立的需求代碼開發。
這樣A庫和B庫的開始時代碼基本相同,但遇到一個問題,有些時候A庫的代碼BUG修改,需要在B庫中再修改一下,不太方便。
因此,就試一下用cherry-pick是否可行。
經過翻看一些前輩文章並開始嘗試,(以把A庫的release的某次提交commit-->commit id is XXXXX,提交到B庫的release分支上為例)步驟如下:
1、把代碼切到B庫的release分支上,為防止出問題,建議先拉取一個備份的分支。
2、本地添加另一個倉庫A
Zxxxxx5:B 1$ git remote add zhorigin http://git.xxxx.com/A.git
通過git remote -v 可以查看是否添加成功
Zxxxxx5:B 1$ git remote -v
origin http://git.xxxx.com/B.git (fetch)
origin http://git.xxxx.com/B.git (push)
zhorigin http://git.xxxx.com/A.git (fetch)
zhorigin http://git.xxxxcom/A.git (push)
3、Zxxxxx5:B 1$ git fetch zhorigin 這時會把遠程A庫的分支信息同步到本地
4、這時候就可以cherry-pick了,遇到相關的提示,根據提示操作就行了
Zxxxxx5:B 1$ git cherry-pick ac50e25bf
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:
git config --global --edit
After doing this, you may fix the identity used for this commit with:
git commit --amend --reset-author
---------------------------------------------------------------
---------------------------------------------------------------
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:
git config --global user.name "Your Name"
git config --global user.email you@example.com
After doing this, you may fix the identity used for this commit with:
git commit --amend --reset-author
5、上面的提示,操作完后,就可以git push了,這時候,神奇的一幕出現了,把修復A庫的BUG分支 clone了一份到B庫上了,(如果在A庫的release分支直接修改的,可能就直接push到B庫的release了,有興趣的可以試一下)
Zxxxxx5:B 1$ git push
Enumerating objects: 69, done.
Counting objects: 100% (69/69), done.
Delta compression using up to 8 threads
Compressing objects: 100% (20/20), done.
Writing objects: 100% (39/39), 4.01 KiB | 2.00 MiB/s, done.
Total 39 (delta 13), reused 10 (delta 3)
To http://git.xxxx.com/B.git
ccccc..yyyyyy bugfix-branch-xxxx-A -> bugfix-branch-xxxx-A
6、至此A庫的BUG修改分支在B庫已經有了,之后的流程就簡單了,和普通的cherry-pick流程一樣了
本地確保切到B庫的release
Zxxxxx5:B 1$ git checkout release
Zxxxxx5:B 1$ git cherry-pick yyyyyy
Zxxxxx5:B 1$ git push
7、去掉remove,避免影響正常的代碼操作
Zxxxxx5:B 1$ git remote -v
origin http://git.xxxx.com/B.git (fetch)
origin http://git.xxxx.com/B.git (push)
zhorigin http://git.xxxx.com/A.git (fetch)
zhorigin http://git.xxxxcom/A.git (push)
Zxxxxx5:B 1$ git remote remove zhorigin
Zxxxxx5:B 1$git remote -v
origin http://git.xxxx.com/B.git (fetch)
origin http://git.xxxx.com/B.git (push)
備注:
這里面有一些細節,很有意思,比如為什么A庫clone出一個分支到B庫,新的分支版本的起始commit id為什么是 ccccc等等