【Git入門之十】Rebase操作
阿新 • • 發佈:2019-02-01
Rebase,衍合?變基?唉,我也不知道要怎麼翻譯合適。。。變基怪怪的,我擦勒,你才變基呢。。。
1.Rebase提交
rebase命令允許你編輯你的提交歷史,比如你可以把多個提交(commit)聯合成一個,可以對它重新排序,跳掉某個提交或者編輯提交資訊。這是非常有用的,當你要再推送到遠端倉庫之前要想重新編輯提交資訊。以下舉個例子把多個提交聯合成一個。
- #做一些無聊的修改和提交
- $ echo "test_rebase1" > jackydata01
- $ git commit -a -m "test_rebase1"
-
[master 128a2f5] test_rebase1
- 1 file changed, 1 insertion(+), 1 deletion(-)
- $ echo "test_rebase2" > jackydata01
- $ git commit -a -m "test_rebase2"
- [master 5ae88f5] test_rebase2
- 1 file changed, 1 insertion(+), 1 deletion(-)
- $ echo "test_rebase3" > jackydata01
- $ git commit -a -m "test_rebase3"
-
[master 37be873] test_rebase3
- 1 file changed, 1 insertion(+), 1 deletion(-)
- $ echo "test_rebase4" > jackydata01
- $ git commit -a -m "test_rebase4"
- [master 7dff3e4] test_rebase4
- 1 file changed, 1 insertion(+), 1 deletion(-)
- $ echo "test_rebase5" > jackydata01
- $ git commit -a -m "test_rebase5"
-
[master 897fa79] test_rebase5
- 1 file changed, 1 insertion(+), 1 deletion(-)
- $ git log --pretty=oneline
- 897fa79def2cf256b7f14d0ec0fcb67e8f61a814 test_rebase5
- 7dff3e4ad7416f0370b4579393a0c8a8fc02efbd test_rebase4
- 37be873b514afd9d8f6536cd893eadc3e32b4966 test_rebase3
- 5ae88f5a0454066d820f6c1687b6d01f59f39dbd test_rebase2
- 128a2f5cdb325bcbdb26b35741fad2a2fba49832 test_rebase1
- ...還有很多
- git rebase -i HEAD~5
2.Rebase分支
對多個分支進行rebase操作,rebase命令為一個分支的更改生成一個補丁,然後把這個補丁應用到另外一個分支上,那麼最後的原始碼和merge是一樣的,使用這種方式進行分支的合併更為合理。- #建立並跳轉到分支br
- $ git branch br
- $ git checkout br
- Switched to branch 'br'
- #修改br分支中的Jackydata01
- $ echo "this wiil be rebase to master" >jackydata01
- #提交br分支中的修改
- $ git commit -a -m "rebase to master"
- [br 5c2a431] rebase to master
- 1 file changed, 1 insertion(+), 1 deletion(-)
- #跳轉到master分支
- $ git checkout master
- Switched to branch 'master'
- Your branch is ahead of 'origin/master' by 1 commit.
- (use "git push" to publish your local commits)
- #使用rebase把br的改變應用到master
- $ git rebase br
- First, rewinding head to replay your work on top of it...
- Fast-forwarded master to br.
- #顯示master中jackydata01的內容
- $ cat jackydata01
- this wiil be rebase to master