git rebase的用法
改变基

一个git库,开发人员在master分支的Bcommit的时候,创建了一个dev分支,此时Bcommit是dev分支的基,然后分别进行两个分支的开发。
进行到master提交了Dcommit,而dev分支提交到了Zcommit,如果此时需要将dev分支的基切换为D,那么可以用下面这个命令:
git checkout dev #切换到dev分支
git rebase master #将master最新的commit作为基
执行这个命令时,可能会有分支冲突,解决冲突之后,进行如下操作:
# 解决冲突
git add xxx
git rebase --continue
进行完这些操作后,分支的情况就如下图了:

使用git log来查看提交日志,可以看到dev分支的x、y、z的提交次序变到了maste分支的Dcommit后面。
也就是说,这里进行了一个git merge。
拓展:如果要将下面的test分支基变为master分支的D,那么可以使用git rebase --onto master dev^ test

合并提交记录
首先看下面这个例子:
[root@centos demo]# git init
Initialized empty Git repository in /root/demo/.git/
[root@centos demo]# echo one >> a.txt
[root@centos demo]# git add a.txt
[root@centos demo]# git commit -m "first commit"
[master (root-commit) b7ee3a2] first commit
1 file changed, 1 insertion(+)
create mode 100644 a.txt
[root@centos demo]# echo two >> a.txt
[root@centos demo]# git commit -am "second commit"
[master 92ae9c4] second commit
1 file changed, 1 insertion(+)
[root@centos demo]# echo three >> a.txt
[root@centos demo]# git commit -am "third commit"
[master 0985eec] third commit
1 file changed, 1 insertion(+)
[root@centos demo]# echo four >> a.txt
[root@centos demo]# git commit -am "four commit"
[master 5bd480c] four commit
1 file changed, 1 insertion(+)
[root@centos demo]# git show-branch --more=4 #查看四次提交记录
[master] four commit
[master^] third commit
[master~2] second commit
[master~3] first commit
上面代码进行了4次提交,但是现在有个需求,将这四次提交的合并为一个提交,提交信息整合一下,改成"four commit"
可以这么做:
[root@centos demo]# git rebase -i master~3 #会启动vi编辑器,顶部显示的内容如下:
pick 92ae9c4 second commit
pick 0985eec third commit
pick 5bd480c four commit
然后将上面这三行中,后面两行的pick替换为squash,即下面这个样子:
pick 92ae9c4 second commit
#将后面两次的pick改成squash,然后保存退出
squash 0985eec third commit
squash 5bd480c four commit
保存退出后,又会打开一个vim编辑器窗口,内容如下:
# This is a combination of 3 commits.
# This is the 1st commit message: second commit # This is the commit message #2: third commit # This is the commit message #3: four commit
从上面三个commit信息,就是会显示在合并后的那个commit提交信息,如果原封不动的话,那么三次提交合并为一个提交之后,这一个提交就会有三行提交comment。
当然,可以使用#来注释其中的某几条comment,或者修改其中的comment,都行。
比如我只想保留最后一次提交comment,那么可以向下面这么做:
# This is a combination of 3 commits.
# This is the 1st commit message: #second commit # This is the commit message #2: #third commit # This is the commit message #3: four commit
然后保存并退出,不出意外的话,现在已经合并提交成功了。
可以查看一下提交记录:
[root@centos demo]# git show-branch --more=4
[master] four commit
[master^] first commit
同时,文件的内容也没有发生改变:
[root@centos demo]# cat a.txt
one
two
three
four
git rebase的用法的更多相关文章
- Git merge && git rebase的用法
Git merge的用法: git merge Dev // Dev表示某分支,表示在当前分支合并Dev分支 git merge -m “Merge from Dev” Dev //-m可以加上m ...
- git命令之git rebase 的用法
rebase 假设你现在基于远程分支"origin",创建一个叫"mywork"的分支. $ git checkout -b mywork origin 现在我 ...
- Git merge 与 git rebase的区别
Git merge的用法: git merge Dev // Dev表示某分支,表示在当前分支合并Dev分支 git merge -m "Merge from Dev" Dev ...
- 使用git Rebase让历史变得清晰
当多人协作开发一个分支时,历史记录通常如下方左图所示,比较凌乱.如果希望能像右图那样呈线性提交,就需要学习git rebase的用法. “Merge branch”提交的产生 我们的工作流程是:修改代 ...
- git rebase和git merge的用法
http://softlab.sdut.edu.cn/blog/subaochen/2016/01/git-rebase%E5%92%8Cgit-merge%E7%9A%84%E7%94%A8%E6% ...
- 记录git rebase用法
git 是基于文件系统的版本管理工具,文档和详细介绍可以查看git 一.git commit --amend 如果你对文件做了修改需要和上一次的修改合并为一个change git add . git ...
- [译]git rebase -i
使用rebase -i会在终端出现一个交互页面. 在这个交互页面中我们可以对要rebase的commit做一定的修改. 用法 git rebase -i <master> 把当前的分支的c ...
- [译]git rebase
rebase就是重新定义你分支的起点, 分支上的commit将生成对应的新的commit并放在你指定的新的起点commit后, 分支上的老commit将被删除. rebase就是将你的分支从一个com ...
- 妙用git rebase --onto指令
有时候,在分支提交更改的时候,会忘记rebase,就直接提交上去,或者忘记和本地远程分支做merge,就直接rebase了别的分支.有时候真希望有一种切片的方式,让自己的分支只需要接上某一段.这个时候 ...
随机推荐
- PCL学习笔记1
先贴一段代码,从别处抄来的 #include <iostream> #include <pcl/io/pcd_io.h> #include <pcl/point_type ...
- LeetCode算法题-Ransom Note(Java实现)
这是悦乐书的第212次更新,第225篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第80题(顺位题号是383).给定一个任意赎金票据字符串和另一个包含所有杂志字母的字符串 ...
- March 08th, 2018 Week 10th Thursday
Easy come, easy go. 易得则易失. Easy come, easy go, I finally undestand the phrase through somewhat hard ...
- February 17th, 2018 Week 7th Saturday
The happiest part of a man's life is what he passes lying awake in bed in the morning. 人生一大乐事莫过去早上醒来 ...
- 用栈来实现队列的golang实现
使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返回队列首部的元素. empty() -- 返回队列是否为空. ...
- https 建立连接过程
http://blog.csdn.net/wangjun5159/article/details/51510594 思考问题的顺序 学技术时,总是会问什么?这里也不例外,https为什么会存在,它有什 ...
- GraphQL 是什么
我的理解,GraphQL 是一种以Json为载体实现:操作数据和获取结果的需求的查询语言!简言:以Json换Json.
- 11.redis_python
# pip install redis import redis # 1.链接数据库 key--value client = redis.StrictRedis(host='127.0.0.1', p ...
- Java学习笔记(二)——类和对象
[1]类是模子,确定对象将会拥有的特征(属性)和行为(方法). [2]类的特点:类是对象的类型: 具有相同属性和方法的一组对象的集合. [3]属性:对象具有的各种特征(每个对象的每个属性都拥有特定值) ...
- docker: read tcp 192.168.7.235:36512->54.230.212.9:443: read: connection reset by peer.
在学习rancher的时候去下载rancher/agent镜像的时候,出现报错:docker: read tcp 192.168.7.235:36512->54.230.212.9:443: r ...