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 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;
}
錯誤被修改過來了!
總結
以上是生活随笔為你收集整理的git rebase 命令 常用_git命令之 git rebase 常用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 消息称理想首款纯电车型 MEGA 将于
- 下一篇: 华为扩大鸿蒙4升级覆盖面 5年前老机型均