失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > git rebase 命令 常用_git命令之 git rebase 常用

git rebase 命令 常用_git命令之 git rebase 常用

时间:2023-12-31 10:31:11

相关推荐

git rebase 命令 常用_git命令之 git rebase 常用

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 Ahello pickB hi pickC how are you pickD i am fine

pickE 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 Ahello

pickD i am fine pickB hi pickC how are you pickE 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 Ahello pickB hi pickC how are you pickD 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 Ahello pickB hi editC how are you pickD bye

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

You can amend the commit now, withgit commit --amend Once you are satisfied with your changes, rungit 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;

}

错误被修改过来了!

如果觉得《git rebase 命令 常用_git命令之 git rebase 常用》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。