多个git项目库之间的git cherry-pick代码同步
这段时间做项目间的代码分离,从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等等
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)