除了以上列出的经常使用的命令外,下面着重结合场景来讲明一下git rebase
命令git
同个功能因一些缘由打断开发,且并无开发完成,此时可能会先提交一个备份的commit
,为了提交简洁,须要将备份的提交合并到一块儿。此时就会用到git rebase -i
命令shell
而后咱们执行vim
1. 执行下面命令(表示合并最近的3次提交)
git rebase -i HEAD~3
2.以后会出现洗面的vim编辑器
pick 9ce5910 tranform to kotlin #
pick 5340ce6 整理狼人杀模块,重构狼人杀主界面
pick a977304 MVVM模式重构狼人杀页面
# Rebase 76ce1a8..a977304 onto 76ce1a8 (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
3. 键盘点击 i 进入编辑模式,按照提示修改第二三次提交的pick为 s(包含) 或者 f。这里不须要提交信息因此改成s
pick 9ce5910 tranform to kotlin #
f 5340ce6 整理狼人杀模块,重构狼人杀主界面
f a977304 MVVM模式重构狼人杀页面
# Rebase 76ce1a8..a977304 onto 76ce1a8 (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
-- INSERT --
4.点击esc,进入vim命令模式,而后输入":wq"回车 。
复制代码
有时咱们同一个版本会有多个分支同时开发,若是采用pull + merge的方式会产生多余的提交记录影响。此时咱们依然能够采用git rebase。bash
# 主分支:dev 功能分支: feature
1. 切换到主分支
git checkout dev
2. 查看主分支是不是最新的
git fetch
git status
3. 若是不是最新的
git pull
4. 切回功能分支
git checkout -
5. rebase 主分支
git rebase -i dev
6. 同第一种合并多个commit场景进行vim操做
7. 若是存在冲突,须要解决冲突后
git rebase --continue
7. rebase成功后,须要强推回本身的远程分支(此处注意若是有冲突建议新建一个分支去操做,保存好代码防止异常出现)
git pull -f
复制代码