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

  

 

转载于:https://www.cnblogs.com/-beyond/p/9495604.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值