- 可以到这里下载安装
Git: git-scm.com/download/wi…- Learn Git Branching游戏在这里:learngitbranching.js.org/?locale=zh_…
Level 5-3 《纠缠不清的分支》
这一关使用cherry-pick,向多个分支复制提交,不仅在游戏里学习,你还可以在真实环境中进行实验。
游戏答案:
# three移动到c2提交
git branch -f three c2
# 切换到one
git checkout one
# 将 c4,c3,c2 按顺序复制到one下面
git cherry-pick c4 c3 c2
# 切换到two
git checkout two
# 将c5,c4,c3,c2按顺序复制到two下面
git cherry-pick c5 c4 c3 c2
准备实验环境
下面命令完整CV到Windows cmd命令行窗口中执行:
# 准备一个空目录
mkdir level-5-3
cd level-5-3
# 初始化本地仓库
git init
# 做两次提交
echo 111>>a.txt
git add .
git commit -m "c0"
echo 222>>a.txt
git add .
git commit -m "c1"
# 创建三个分支,但不切换分支
git branch one
git branch two
git branch three
# 在master提交4次
echo 333>>a.txt
git add .
git commit -m "c2"
echo 444>>a.txt
git add .
git commit -m "c3"
echo 555>>a.txt
git add .
git commit -m "c4"
echo 666>>a.txt
git add .
git commit -m "c5"
# 查看提交树
git log --graph --pretty=oneline --all
真实答案:
# 将three移动到c2分支,这里使用c2的ID
git branch -f three 9a91
# 切换到one
git checkout one
# 将c4,c3,c2复制到one下面,这里使用三个提交的ID
git cherry-pick 5a59 e5a5 9a91
# 手动修正a.txt后再继续
git add .
git commit --allow-empty --no-edit
# c4复制完成,继续下一步:复制c3
git cherry-pick --continue
# 手动修正a.txt后再继续
git add .
git commit --allow-empty --no-edit
# c3复制完成,继续下一步:复制c2
git cherry-pick --continue
# 手动修正a.txt后再继续
git add .
git commit --allow-empty --no-edit
# c2复制完成,到此,c4,c3,c2全部复制完成
# 查看提交树
git log --graph --pretty=oneline --all
# 切换到two
git checkout two
# 复制c5,c4,c3,c2到two下面,这里使用4个提交的ID
git cherry-pick 1b9b 5a59 e5a5 9a91
# 手动修正a.txt后再继续
git add .
git commit --allow-empty --no-edit
# c5复制完成,继续下一步:复制c4
git cherry-pick --continue
# 手动修正a.txt后再继续
git add .
git commit --allow-empty --no-edit
# c4复制完成,继续下一步:复制c3
git cherry-pick --continue
# 手动修正a.txt后再继续
git add .
git commit --allow-empty --no-edit
# c3复制完成,继续下一步:复制c2
git cherry-pick --continue
# 手动修正a.txt后再继续
git add .
git commit --allow-empty --no-edit
# c2复制完成,到此,c5,c4,c3,c2全部复制完成
# 查看提交树
git log --graph --pretty=oneline --all