- 可以到这里下载安装
Git: git-scm.com/download/wi…- Learn Git Branching游戏在这里:learngitbranching.js.org/?locale=zh_…
Level 3-2 《交互式Rebase》
这一关使用交互式rebase(interactive rebase)命令来移动提交,不仅在游戏里学习,你还可以在真实环境中进行实验。
游戏答案:
git rebase -i HEAD~4
# 或者:
git rebase -i overHere
然后,按下面方式设置:
准备实验环境
下面命令完整CV到Windows cmd命令行窗口中执行:
# 准备一个空目录
mkdir level-3-2
cd level-3-2
# 初始化本地仓库
git init
# 在master分支做两次提交
echo 111>>a.txt
git add .
git commit -m "c0"
echo 222>>a.txt
git add .
git commit -m "c1"
# 创建overHere分支,但不切换到该分支
git branch overHere
# 在master分支继续做四次提交
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
真实答案:
# 编辑器设置为notepad
set GIT_EDITOR=notepad
git rebase -i overHere
在弹出的记事本中,按下面方式修改:
保存并关闭记事本后,会显示提示如下:
这一步移动了c3提交,但是这里有文件冲突,所以在这一步暂停,等待手动修正文件后再继续完成对c5和c4的移动。
按提示修正a.txt后提交,完成第一步rebase c3的操作。然后再继续进行rebase。
c5和c4操作也是完全相同的。
# 先修正a.txt,然后再执行:
git add .
# 使用c3的提交信息,不再重新编辑
git commit --no-edit
# 继续rebase c5
git rebase --continue
# 手动修正a.txt,然后再执行:
git add .
# 使用c5的提交信息,不再重新编辑
git commit --no-edit
# 继续rebase c4
git rebase --continue
# 手动修正a.txt,然后再执行:
git add .
# 使用c4的提交信息,不再重新编辑
git commit --no-edit
查看提交记录和仓库状态:
git log --graph --pretty=oneline --all
git status
按照提示,最后再执行一次git rebase --continue,然后再查看提交记录和仓库状态:
# 最终完成rebase
git rebase --continue
# 查看提交树
git log --graph --pretty=oneline --all
# 查看仓库状态
git status