共计 1059 个字符,预计需要花费 3 分钟才能阅读完成。
最近新学习到的 Git 技术。场景是这样的,我们刚刚又产生了次新的 commit。突然发现上一次,上上次,甚至是很久之前的一次 commit 有问题,我们想修改。
这时候可以使用
git rebase -i
也就是原本 rebase 在加一个参数 -i
假如你有这样的提交历史
PS D:\Develop\study\git_study> git log --oneline
b5c5efd (HEAD -> test/rebaseA) feat:revise the file to generate a new commit
307840b (origin/test/rebaseA) feat: add txt file to study rebase -i
60bb3a7 (origin/master, origin/HEAD, master) 删除了一些 dom
69e0cc8 开发者 A 设计了首页文件
cd62850 Merge branch 'master' of https://gitee.com/bjxwl/git_study
我们想修改 307840b 这次 commit,那么我们可以
git rebase -i HEAD^^ // 注意这里 ^ 的数量 代表偏移的数量
// 或者
git rebase -i HEAD~2 // 相同的效果 写法不一样,直接给出偏移的数量
这时候 会进入 vim 编辑器模式。单击键盘 i 键 可以进入编辑模式
pick 307840b # feat: add txt file to study rebase -i
pick e8deb05 # 进行了 rebase 然后修改了倒数第二个记录
# Rebase 60bb3a7..e8deb05 onto 60bb3a7 (2 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
进入编辑模式 将需要修正的的 commit 哈希前的 pick 改成 edit 进入编辑模式。
工作区当前代码就是对应哈希的 commit 的代码记录。进行修改时可能需要处理冲突
在处理完毕以后我们执行
git add .
git commit -m "xxx"
git rebase --continue // 继续 rebase 流程
最终不会新增一条 commit 记录。会保留原本 commit 历史。当然 这时候是可以对 commit 说明进行修改的。
对比 commit -amend
该 commit 加了参数 -amend 意为修正,只能 对最新一条 commit 进行修正 然后将其替换成最新修改的
但不能修改再往前的 commit 了。
总结
这次我们学习了 rebase 的新用法。
正文完
文章二维码