Git rebase使用
rebase的优点和缺点
优点
- rebase最大的好处是你的项目历史会非常整洁
- rebase 导致最后的项目历史呈现出完美的线性——你可以从项目终点到起点浏览而不需要任何的 fork。这让你更容易使用 git log、git bisect 和 gitk 来查看项目历史
缺点
- 安全性,
如果你在公共分支上使用rebase,重写项目历史可能会给你的协作工作流带来灾难性的影响 - 可跟踪性,
rebase会更改历史记录,rebase 不会有合并提交中附带的信息——你看不到 feature 分支中并入了上游的哪些更改
分支内合并多个commit为一个新commit使用:
命令:
这里我们使用命令:
git rebase -i [startpoint] [endpoint]
其中-i的意思是--interactive,即弹出交互式的界面让用户编辑完成合并操作,[startpoint] [endpoint]则指定了一个编辑区间,如果不指定[endpoint],则该区间的终点默认是当前分支HEAD所指向的commit(注:该区间指定的是一个前开后闭的区间)。
使用:
log日志:
D:\Git\shell-demo (master -> origin)
$ git l
* d7a7c8f - (HEAD -> master) cc (33 minutes ago) | hongda
* dbe4bac - bb (33 minutes ago) | hongda
* 439e203 - aa (34 minutes ago) | hongda
* 5b38c9e - init (8 months ago) | hongqi
执行rebase命令:
$ git rebase -i 5b38c9e
或者:
$ git rebase -i HEAD~3
弹出的vim界面:
pick 439e203 aa
pick dbe4bac bb
pick d7a7c8f cc
# Rebase 5b38c9e..d7a7c8f onto 5b38c9e (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
# 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.
#
# Note that empty commits are commented out
D:/Git/shell-demo/.git/rebase-merge/git-rebase-todo [unix] (16:23 24/12/2018)
上面未被注释的部分列出的是我们本次rebase操作包含的所有提交,下面注释部分是git为我们提供的命令说明。每一个commit id 前面的pick表示指令类型,git 为我们提供了以下几个命令:
- pick:保留该commit(缩写:p)
- reword:保留该commit,但我需要修改该commit的注释(缩写:r)
- edit:保留该commit, 但我要停下来修改该提交(不仅仅修改注释)(缩写:e)
- squash:将该commit和前一个commit合并(缩写:s)
- fixup:将该commit和前一个commit合并,但我不要保留该提交的注释信息(缩写:f)
- exec:执行shell命令(缩写:x)
- drop:我要丢弃该commit(缩写:d)
修改为:
pick 439e203 aa
s dbe4bac bb
s d7a7c8f cc
在弹出下面的界面,可以修改注释
# This is a combination of 3 commits.
# This is the 1st commit message:
modify file
# This is the commit message #2:
bb
# This is the commit message #3:
cc
# Please enter the commit message for your changes. Lines starting
再次查看日志:
D:\Git\shell-demo (master -> origin)
$ git l
* 7e09cf2 - (HEAD -> master) modify file (69 seconds ago) | hongda
* 5b38c9e - init (8 months ago) | hongqi
多个commitid合并成了一个新的commitid
将其他分支合并到主分支,表现为线性:
mm分支:
D:\Git\shell-demo (mm -> origin)
$ git l
* 01dc337 - (HEAD -> mm) c (7 seconds ago) | hongda
* 1719011 - b (18 seconds ago) | hongda
* 588069c - a (34 seconds ago) | hongda
* 5b38c9e - init (8 months ago) | hongqi
master分支:
D:\Git\shell-demo (master -> origin)
$ git l
* 5b38c9e - init (8 months ago) | hongqi
合并:
D:\Git\shell-demo (master -> origin)
$ git rebase mm
First, rewinding head to replay your work on top of it...
Fast-forwarded master to mm.
D:\Git\shell-demo (master -> origin)
$ git l
* 01dc337 - (HEAD -> master, mm) c (57 seconds ago) | hongda
* 1719011 - b (68 seconds ago) | hongda
* 588069c - a (84 seconds ago) | hongda
* 5b38c9e - init (8 months ago) | hongqi
如果rebase有冲突,跟merge一样解决冲突提交即可。
将其他分支多个commit合并到主分支,并形成一个新commit:
我不建议这么使用,除非这里的分支合并完以后就不使用了,不然的话,以后有新的提交,再次合并,很可能忘记从那个commit开始合并。
命令:
我们使用命令的形式为:
git rebase [startpoint] [endpoint] --onto [branchName]
其中,[startpoint] [endpoint]仍然和上一个命令一样指定了一个编辑区间(前开后闭),--onto的意思是要将该指定的提交复制到哪个分支上。
所以,在找到C(90bc0045b)和E(5de0da9f2)的提交id后,我们运行以下命令:
git rebase 90bc0045b^ 5de0da9f2 --onto master
注:因为[startpoint] [endpoint]指定的是一个前开后闭的区间,为了让这个区间包含C提交,我们将区间起始点向后退了一步
使用:
mm分支:
D:\Git\shell-demo (mm -> origin)
$ git l
* 88dc407 - (HEAD -> mm) e (3 seconds ago) | hongda
* 9cc37e9 - d (14 seconds ago) | hongda
* 01dc337 - c (2 hours ago) | hongda
* 1719011 - b (2 hours ago) | hongda
* 588069c - a (2 hours ago) | hongda
* 5b38c9e - init (8 months ago) | hongqi
master分支:
$ git l
* 41fd42b - (HEAD -> master) modify file for aa commit (39 seconds ago) | hongda
* 5b38c9e - init (8 months ago) | hongqi
rebase命令:
git rebase 588069c 88dc407 --onto master
合并b,c,d,e四个提交到master,注意并不包括a
First, rewinding head to replay your work on top of it...
Applying: b
Using index info to reconstruct a base tree...
M asd.txt
Falling back to patching base and 3-way merge...
Auto-merging asd.txt
CONFLICT (content): Merge conflict in asd.txt
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch' to see the failed patch
Patch failed at 0001 g
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".
如果合并过程中遇到冲突,就解决冲突,并add,再继续rebase
D:\Git\shell-demo (HEAD detached at 80f8fc3 -> origin)
$ git aa
D:\Git\shell-demo (HEAD detached at 80f8fc3 -> origin)
$ git rebase --continue
多个合并冲突就会合并多次,合并多次就会产生多个commit,跟我想只有一个commit的意愿违背,非常不喜欢
注意,冲突就会切换到游离分支,解决办法就是创建一个临时分支,用完再删除。
参考:
Git rebase使用的更多相关文章
- git rebase
git rebase -i HEAD~[number_of_commits] git rebase -i HEAD~2
- git rebase与 git合并(error: failed to push some refs to)解决方法
1.遇到的问题 本地有一个git仓库,在github上新建了一个空的仓库,但是更新了REWADME.md的信息,即在github上多了一个提交. 关联远程仓库,操作顺序如下: git remote a ...
- 聊下 git rebase -i
在使用git作为源代码管理工具的时候,开发的时经常会面临一个常见的问题,多个commit 需要合并为一个完整的commit提交. 在一个基本的迭代周期里,你会有很多次commit,有跟配置文件相关的, ...
- [git]rebase和merge
转自:http://blog.csdn.net/wh_19910525/article/details/7554489 Git merge是用来合并两个分支的. git merge b # 将b分支合 ...
- git merge 和 git rebase 小结
Git merge是用来合并两个分支的. git merge b # 将b分支合并到当前分支 同样 git rebase b,也是把 b分支合并到当前分支 ---------------------- ...
- [译]git rebase -i
使用rebase -i会在终端出现一个交互页面. 在这个交互页面中我们可以对要rebase的commit做一定的修改. 用法 git rebase -i <master> 把当前的分支的c ...
- [译]git rebase
rebase就是重新定义你分支的起点, 分支上的commit将生成对应的新的commit并放在你指定的新的起点commit后, 分支上的老commit将被删除. rebase就是将你的分支从一个com ...
- git rebase简介(高级篇)
原文:http://gitbook.liuhui998.com/4_3.html 一.基本 对于git rebase,你亦可以选择进行交互式的rebase.这种方法通常用于在向别处推送提交之前对它 ...
- git rebase简介(基本篇)
原文: http://gitbook.liuhui998.com/4_2.html 一.基本 git rebase用于把一个分支的修改合并到当前分支. 假设你现在基于远程分支"origin& ...
- Git merge 与 git rebase的区别
Git merge的用法: git merge Dev // Dev表示某分支,表示在当前分支合并Dev分支 git merge -m "Merge from Dev" Dev ...
随机推荐
- CRUD简单查询
一.查询所有数据 select * from car 二.查询指定列 select code , price from car 三.修改查询出的列名 select code as '代号' , nam ...
- Linux 查看磁盘或文件夹及文件大小
当磁盘大小超过标准时会有报警提示,这时如果掌握df和du命令是非常明智的选择. df可以查看一级文件夹大小.使用比例.档案系统及其挂入点,但对文件却无能为力. du可以查看文件及文件夹的大小. ...
- hdu5489 树状数组+dp
2015-10-06 21:49:54 这题说的是个给了一个数组,然后删除任意起点的一个连续的L个数,然后求最长递增子序列<是递增,不是非递减>,用一个树状数组维护一下就ok了 #incl ...
- sitecore系统教程之内容编辑器中创建项目
在内容编辑器中创建新项目时,必须先在内容树中选择一个项目,以指示新项目的位置.您可以创建一个新项目作为您选择的项目的兄弟或子项目: 兄弟是您在与所选项目相同的级别创建的项目. 子项是您在所选项下创建的 ...
- MVC请求管道
下面是请求管道中的19个事件. (1)BeginRequest: 开始处理请求 (2)AuthenticateRequest授权验证请求,获取用户授权信息 (3):PostAuthenticateRe ...
- 【Hadoop学习之十三】MapReduce案例分析五-ItemCF
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-3.1.1 推荐系统——协同过滤(Collab ...
- xml 的 <![CDATA["URL"]]>
<![CDATA["URL"]]>:用于 xml 处理特殊字符,比如:& <PolicyURL><![CDATA[ http://ectp.t ...
- (Review cs231n) Gradient Vectorized
注意: 1.每次更新,都要进行一次完整的forward和backward,想要进行更新,需要梯度,所以你需要前馈样本,马上反向求导,得到梯度,然后根据求得的梯度进行权值微调,完成权值更新. 2.前馈得 ...
- 前端框架VUE----babel
这个是解析我们es6的代码的,为什么要用它呢,因为对于一些ie浏览器,甚至FF浏览器,低版本的还不能识别我们的es6代码,那么vue里面好多还让我们去写es6的代码,这个时候我们就可以用babel这个 ...
- Python进阶【第三篇】Python中的基本数据类型
一.运算符 1.算术运算 2.比较运算 3.赋值运算 4.逻辑运算 5.成员运算 二.基本数据类型 1.数字 int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1 ...