分享

git rebase 的活用

 灬木木的花灬 2015-08-12



转载请注明出处:Alvin Lee的博客:http://blog.csdn.net/lihaoweiV


git rebase 命令的功能很强大, 在《git 权威指南》一书里,改变提交(commit)的顺序,书中教了一种很复杂的方法,运用了git reset ... git cherry-pick...等等命令。

但如果用git rebase 命令,则一下就搞定。

以下面的例子来讲解一下git rebase 的其中一个用法,

************************************* 改变提交(commit) 的顺序 ****************************************

git log一下查看commit log:

现有:

commit A  hello    (这里用字母ABCDE代替了那串很长的Hash code)

commit B  hi

commit C  how are you

commit D  i am fine

commit E  bye

现在想将D换到B之前,即 A, D, B, C, E,

我们可以用 git rebase -i [commit号] 命令,  因为现在要改变D的位置, 所以我们要rebase到commit E那里, 具体命令为:

git rebase -i E

按回车后会出现下面的文字

  pick A hello
  pick B  hi
  pick C  how are you
  pick D  i am fine

  pick E  bye
 
  # Rebase 23350be..92c4c19 onto 23350be   (以下部分可能会根据情况不一样而不同)
   #
   # Commands:
   #  p, pick = use commit
  #  r, reword = use commit, but edit the commit message
  #  e, edit = use commit, but stop for amending
  #  s, squash = use commit, but meld into previous commit
  #  f, fixup = like "squash", but discard this commit's log message
  #
  # If you remove a line here THAT COMMIT WILL BE LOST.
  # However, if you remove everything, the rebase will be aborted.
  #



要将D弄到B前面,只需将上面的pick语句的位置换一下,换成如下

 

  pick A hello

  pick D  i am fine
  pick B  hi
  pick C  how are you
  pick E  bye


然后保存退出,如果你确定位置置换了之后不会发生冲突,就会出现以下字样表示成功了

successfully rebased and updated refs/heads/[你的branch名].


你再git log一下,看看现在的顺序为

commit A  hello 

commit D  i am fine

commit B  hi

commit C  how are you

commit E  bye


当然, 你可以用此方法置换成任何你想要的顺序,但你得先确定,换位后不会发生冲突。如果有冲突,是不会出现successfully的字样的。



以下再说另一种git rebase的用法

**************** 修改提交(非置顶的提交)内容(包括标题,作者,代码等)并更新提交 ************

现有

commit A hello

commit B hi

commit C how are u

commit D bye


我想修改commit C的代码 和标题,但我又不想用git reset 命令这么麻烦,

这里也可以用git rebase -i [commit号] 命令

具体为:

git rebase -i D ,       因为我要修改C,所以我要rebase 到C的前一个commit,即D。

按回车后仍然会看到像上面一样的文字


 

  pick A hello
  pick B  hi
  pick C  how are you
  pick D  bye

 
  # Rebase 23350be..92c4c19 onto 23350be   (以下部分可能会根据情况不一样而不同)
   #
   # Commands:
   #  p, pick = use commit
  #  r, reword = use commit, but edit the commit message
  #  e, edit = use commit, but stop for amending
  #  s, squash = use commit, but meld into previous commit
  #  f, fixup = like "squash", but discard this commit's log message
  #
  # If you remove a line here THAT COMMIT WILL BE LOST.
  # However, if you remove everything, the rebase will be aborted.
  #


这里将C 前面的pick 改为edit ,即

  pick A hello
  pick B  hi
  edit C  how are you
  pick D  bye

然后保存退出,就会出现以下字样:

You can amend the commit now, with


        git commit --amend


Once you are satisfied with your changes, run


        git rebase --continue


现在你想修改什么内容? 


如果你想修改commit 的标题, 备注, 作者签名等信息,请用

git commit --amend 命 令

例如,我要将commit C的标题 改为 Hello, I m Alvin Lee. 用上述命令修改完之后保退出去,

然后再用git rebase --continue使你的修改生效。


如果你发现commit C代码有bug,想修改那个bug,例如driver/alvin.c 里有个bug, 则直接打开该文件:

int main(void)

{

prinntk("Hello I am Alvin Lee!\n")

return 1;

}


将错误处修改:

printk("Hello I am Alvin Lee!\n");

保存退出。 用git add 命令将你的修改添加到暂存区(index),

再用git rebase --continue命令使你的修改生效,

如果没有冲突, 则一切OK!

现在用git log -p [commit号] 命令看一下,

int main(void)

{

printk("Hello I am Alvin Lee!\n");

return 1;

}

错误被修改过来了!



******************************************* 更多用法, 且听下回分解 ***************************************************




 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多