1.rebase(变基)操作

注意事项:rebase 改变分支的根源,绝对不要在与其他人共享的分支上进行操作rebase黄金法则:绝不要在公共的分支上使用它!

1.1git merge 与 git rebase的区别

1.1.1git merge 合并两个分支并生成一个新的提交

1.1.2git rebase提取操作有点像git cherry-pick一样,执行rebase后依次将当前(执行rebase时所在分支)的提交cherry-pick到目标分支(待rebase的分支)上,然后将在原始分支(执行rebase时所在分支)上的已提交的commit删除。

###1.1.3 merge结果能够体现出时间线,但是rebase会打乱时间线
在项目中经常使用git pull来拉取代码,git pull相当于是git fetch + git merge;
在项目中运行git pull -r,也就是git pull --rebase,相当于git fetch + git rebase;

1.2当前分支master

1.3基于当前的master分支创建一个rebase_dev

1.4基于rebase_dev分支增加两次提交

1.5切回master分支,增加两次新的提交

1.6切回rebase_dev,查看当前git log

1.7在rebase_dev分支上进行变基操作

$git rebase master



可以看到rebase的操作,打乱了时间线;版本树形成一条

1.8回顾一下merge的操作

1.8.1初始化一个仓库,基于master,增加两次提交

1.8.2基于master分支,创建merge_dev分支,增加两次提交

1.8.3基于merge_dev,切换至master分支

1.8.4基于master分支增加一次提交,而后切换至merge_dev


1.8.5将master分支合并至merge_dev并完成提交



2.git rebase -i 命令操作

usage: git rebase [-i] [options] [--exec <cmd>] [--onto <newbase> | --keep-base] [<upstream> [<branch>]]or: git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] --root [<branch>]or: git rebase --continue | --abort | --skip | --edit-todo--onto <revision>     rebase onto given branch instead of upstream--keep-base           use the merge-base of upstream and branch as the current base--no-verify           allow pre-rebase hook to run-q, --quiet           be quiet. implies --no-stat-v, --verbose         display a diffstat of what changed upstream-n, --no-stat         do not show diffstat of what changed upstream--signoff             add a Signed-off-by trailer to each commit--committer-date-is-author-datemake committer date match author date--reset-author-date   ignore author date and use current date-C <n>                passed to 'git apply'--ignore-whitespace   ignore changes in whitespace--whitespace <action>passed to 'git apply'-f, --force-rebase    cherry-pick all commits, even if unchanged--no-ff               cherry-pick all commits, even if unchanged**--continue            continue**--skip                skip current patch and continue--abort               abort and check out the original branch--quit                abort but keep HEAD where it is** --edit-todo           edit the todo list during an interactive rebase**--show-current-patch  show the patch file being applied or merged--apply               use apply strategies to rebase-m, --merge           use merging strategies to rebase-i, --interactive     let the user edit the list of commits to rebase--rerere-autoupdate   update the index with reused conflict resolution if possible--empty <{drop,keep,ask}>how to handle commits that become empty--autosquash          move commits that begin with squash!/fixup! under -i-S, --gpg-sign[=<key-id>]GPG-sign commits--autostash           automatically stash/stash pop before and after-x, **--exec <exec>     add exec lines after each commit of the editable list**-r, --rebase-merges[=<mode>]try to rebase merges instead of skipping them--fork-point          use 'merge-base --fork-point' to refine upstream-s, --strategy <strategy>use the given merge strategy-X, --strategy-option <option>pass the argument through to the merge strategy--root                rebase all reachable commits up to the root(s)--reschedule-failed-execautomatically re-schedule any `exec` that fails--reapply-cherry-picksapply all changes, even those already present upstream# 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
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.

变基时可用的命令:pick,reword,edit,squash,fixup,exec

2.1准备工作,初始化仓库,添加文件,提交,基于master分支创建rebase_i分支

2.2pick 更改提交顺序、删除提交

2.2.1假定,我们现在要改变提交 M4.txt 和 M3.txt 的顺序,该怎么操作?

2.2.1.1M3.txt是倒数第二次提交,告诉git 我要改变倒数第2次后的提交

$git rebase -i HEAD~2

接着,git给你一个文本,告诉你我知道了,下面注释的是提示,我们不需要管,只要专注前两行就ok

2.2.1.2把第一行和第二行交换顺序.


接着 Esc,:wq 保存退出

2.2.1.3git log 查看更改


2.2.2假定,我们现在要删除某一个提交(假设是M4),该怎么操作?

2.2.1.1M4.txt是排序后的倒数第二次提交,告诉git我要改变倒数第2次后的提交

$ git rebase -i HEAD~2

2.2.2.2删除M4的信息:pick 14929f1 添加了一个M4.txt

11.2.2.3git log 查看更改


2.3record 修改提交消息(提交内容不变)

2.3.1假定,我们现在要修改某个提交的消息(M2),该怎么操作?

2.3.1.1git log 查看 M2 距离 HEAD 有多少的距离,得:2

git rebase -i HEAD~2

2.3.1.2只需要修改M2 ,pick 为 r 是 record简写

2.3.1.3接着 Esc,:wq 保存退出

2.3.1.4git会说 开始执行,接着弹出一个编辑窗口


接着 Esc,:wq 保存退出

2.3.1.5git log 查看, 消息 “添加了一个M2.txt” 变为了 “重新修改commitM2,添加了一个M2.txt”


【此种操作顺带的也会改变commit ID号】

2.4edit修改提交

2.4.1假定 我想要在两个提交之间 再加提交,怎么办?

2.4.1.1假定,我们要在 M2 和 M3之间 添加一条提交

git rebase -i HEAD~2

2.4.1.2只需要修改M2 ,pick 为 e 是 edit简写

2.4.1.3rebase_i分支多了REBASE-i 1/2

2.4.1.4查看当前变基操作的状态


2.4.1.5给M2.txt 增加一些内容,然后提交

2.4.1.6提交完成之后,查看当前的状态,并根据提供的选择进行操作(演示选择:git rebase --continue)

#使用“git rebase——edit-todo”查看和编辑
use "git rebase --edit-todo" to view and edit
You are currently editing a commit while rebasing branch 'rebase_i' on '9119c19'.
#您当前正在编辑提交,同时重新建立分支
#使用"git commit——amend"修改当前提交
use "git commit --amend" to amend the current commit
#当你对你的更改感到满意时,使用“git rebase——continue”
use "git rebase --continue" once you are satisfied with your changes

2.4.1.7查看当前提交日志是否满足需求



2.5.1假定 我想要单纯的修改这次提交内容和消息,怎么办?

可参见上述操作步骤进展至11.4.1.3rebase_i分支多了REBASE-i 1/2,11.4.1.6使用"git commit——amend"修改当前提交

2.5.1.1使用"git commit——amend"修改当前提交

2.5.1.2进入修改界面,完成修改后 按ESC,:wq

2.5.1.3继续进行变基操作

$ git rebase --continue

2.5.1.4查看log


2.6.1假定,我想合并某几个提交,怎么办?(squash合并提交)

2.6.1.1合并 M1 和 M2

修改为


开始执行变更然后 在弹出来的编辑框里 写提交信息,可以修改提交消息,默认是把两个消息都合并

接着 Esc,:wq 保存退出,git log查看,合并成功

======================================================================
创作不易,本人热衷开源共享 《git rebase(变基)操作演示》
======================================================================

转载请附上链接:
https://www.cnblogs.com/cndevops/p/14993331.html

git rebase(变基)操作演示相关推荐

  1. git rebase 变基命令——移花接木

    git rebase 变基命令--移花接木 语法 描述 git rebase <since> [<till>]​ git rebase --onto <newbase&g ...

  2. git Rebase 变基 教程

    在上一节我们看到了,多人在同一个分支上协作时,很容易出现冲突.即使没有冲突,后push的童鞋不得不先pull,在本地合并,然后才能push成功. 每次合并再push后,分支变成了这样: $ git l ...

  3. git rebase 变基

    概念 变基(Rebase)也是合代码的一种手段. 变基与合并(Merge)不同的是,他可以修改历史,使用rebase来代替merge合代码的话,得到的历史记录是一条直线提交历史,无分叉,很漂亮. 然而 ...

  4. Git 分支 - rebase 变基

    变基 在 Git 中整合来自不同分支的修改主要有两种方法:merge 以及 rebase. 在本节中我们将学习什么是"变基",怎样使用"变基",并将展示该操作的 ...

  5. Git 分支 - 变基示例操作

    目录 变基 变基的基本操作 更有趣的变基例子 变基的风险 用变基解决变基 变基 vs. 合并 变基 在 Git 中整合来自不同分支的修改主要有两种方法:merge 以及 rebase. 在本节中我们将 ...

  6. 3.6 Git 分支 - 变基

    变基 在 Git 中整合来自不同分支的修改主要有两种方法:merge 以及 rebase. 在本节中我们将学习什么是"变基",怎样使用"变基",并将展示该操作的 ...

  7. Git 分支 - 变基

    2019独角兽企业重金招聘Python工程师标准>>> 变基 在 Git 中整合来自不同分支的修改主要有两种方法:merge 以及 rebase. 在本节中我们将学习什么是" ...

  8. 6 Git 分支 - 变基

    变基 在 Git 中整合来自不同分支的修改主要有两种方法:merge 以及 rebase.在本节中我们将学习什么是"变基",怎样使用"变基",并将展示该操作的惊 ...

  9. 最简单的git merge 和git rebase 介绍和示例演示

    merge与rebase merge与rebase 是git 中用来整合不同分支的两种方式. merge, 合并 rebase, 变基 ,也称为衍合. 相比merge, rebase最大的优势就是提交 ...

最新文章

  1. Java通过JDBC连接MySQL数据库
  2. 转:从零开始做app需要做的事情列表
  3. 折半查找递归算法_两篇文章带你了解java基础算法之递归和折半查找
  4. 通过学历造假获得面试机会,并成功拿到 Offer,这样的操作你认可吗?
  5. CSP认证201412-4 最优灌溉[C++题解]:最小生成树裸题、Kruskal算法求最小生成树
  6. Cookie ,Session
  7. iostext添加点击事件_iOS实现一段文字中指定的某些文字点击有响应事件或者可以跳转(给字符串添加超链接)...
  8. Linux的sort命令用法
  9. 新来乍到,谢谢大家捧场
  10. 《国境的南边》观后感
  11. linux新手入门学习 - linux目录结构
  12. 值得关注的18支基金
  13. 3D NAND“大连造”
  14. 【围棋游戏——使用Python实现(纯tkinter gui)】
  15. [NOIp 2014]解方程
  16. 卸载windows10强推的新版edge浏览器
  17. 一定要小心AI语音合成技术,我妈就被骗了!
  18. 预训练综述 Pre-trained Models for Natural Language Processing: A Survey 阅读笔记
  19. 网站建设安全隐患有哪些,网站如何确保安全性?
  20. 虚拟局域网vlan以及技术

热门文章

  1. 软件开发项目管理工具哪个好?
  2. PMP 备考知识点集锦
  3. 一文看懂WebRTC媒体服务器
  4. 计算机管理恢复分区,windows10系统隐藏恢复分区Recovery Image的方法
  5. 四种常见背包问题整理
  6. 【Markdown】Markdown 中的Flow flowchart.js 的基础教程
  7. PHP面试题(遇到的)
  8. 计算机微课用什么音乐,有关微课音乐课堂教学
  9. 关于在word中插入页码以及目录的操作
  10. 移动端vue仿朋友圈项目总结