git repoA cp本地repoB的指定commit

方法一:添加 remote + cherry-pick(推荐)

# 1. 进入目标仓库 repoB
cd /path/to/repoB

# 2. 把 repoA 添加为 remote(只读,不 push)
git remote add repoA /path/to/repoA

# 3. 拉取 repoA 的所有分支信息
git fetch repoA

# 4. 查看 repoA 指定分支上的提交历史,找到你要 cherry-pick 的 commit hash
git log repoA/<branch-name> --oneline

# 5. cherry-pick 单个或多个提交
git cherry-pick <commit-hash-1>
git cherry-pick <commit-hash-2> <commit-hash-3> # 多个

# 6. (可选)完成后清理 remote
git remote remove repoA

方法二:直接路径引用(不需要 remote)

# 如果 repoA 已经 fetch 过,repoB 可以直接引用它的对象
cd /path/to/repoB

# 直接从 repoA 的 .git 目录读取指定分支的提交
git --git-dir=/path/to/repoA/.git log <branch-name> --oneline

# cherry-pick 时指定来源
git cherry-pick <commit-hash>
# 如果遇到对象找不到,先:
GIT_ALTERNATE_OBJECT_DIRECTORIES=/path/to/repoA/.git/objects git cherry-pick <commit-hash>