git rebase
分支合并命令,但区别在于它修改了提交的顺序
Git 试图将其他分支的提交放在当前本地分支的HEAD之上
如,本地分支A->B->C->D提交;而合并分支有A->B->X->Y的提交,那么git merge会将当前本地分支转换为A->B->C->D->X->Y
Git rebase命令试图找出当前本地分支和合并分支之间的共同祖先。然后,它通过修改当前本地分支中的提交顺序,将提交推送到本地分支
如,本地分支A->B->C->D提交;而合并分支有A->B->X->Y的提交,那么git rebase会将当前本地分支转换为A->B->X->Y->C->D
当开发者在一个远程仓库工作时,你不能修改远程仓库中的提交顺序。在这种情况想,使用rebase操作,把本地提交放在远程仓库的提交之上,你可以推送这些修改。
作用: 把价差的提交历史“整理”成一条直线,看上去更直观
缺点:本地交叉提交已经被修改过
- merge
# step1:main与test_branch分支已同步
29447@GW64 /d/myProject (main)
$ git log main -2 --oneline
989cbae (HEAD -> main, origin/main, origin/HEAD, test_branch) delete patch files
dc06cd4 (tag: v2.0, origin/test_branch, dev_branch) test patch again
29447@GW64 /d/myProject (main)
$ git log test_branch -2 --oneline
989cbae (HEAD -> main, origin/main, origin/HEAD, test_branch) delete patch files
dc06cd4 (tag: v2.0, origin/test_branch, dev_branch) test patch again
29447@GW64 /d/myProject (main)
$ git checkout test_branch
Switched to branch 'test_branch'
Your branch is ahead of 'origin/test_branch' by 1 commit.
(use "git push" to publish your local commits)
# step2:test_branch分支提交c、d
29447@GW64 /d/myProject (test_branch)
$ git log test_branch -2 --oneline >c
29447@GW64 /d/myProject (test_branch)
$ git add .
warning: in the working copy of 'c', LF will be replaced by CRLF the next time Git touches it
29447@GW64 /d/myProject (test_branch)
$ git commit -m "test_branch add c"
[test_branch 409dc16] test_branch add c
1 file changed, 2 insertions(+)
create mode 100644 c
29447@GW64 /d/myProject (test_branch)
$ git log test_branch -2 --oneline >d.txt
29447@GW64 /d/myProject (test_branch)
$ git add .
warning: in the working copy of 'd.txt', LF will be replaced by CRLF the next time Git touches it
29447@GW64 /d/myProject (test_branch)
$ git commit -m "test_branch add d"
[test_branch b581603] test_branch add d
1 file changed, 2 insertions(+)
create mode 100644 d.txt
29447@GW64 /d/myProject (test_branch)
$ git log test_branch -2 --oneline
b581603 (HEAD -> test_branch) test_branch add d
409dc16 test_branch add c
# step2:main分支提交x,y
29447@GW64 /d/myProject (test_branch)
$ git checkout main
Switched to branch 'main'
29447@GW64 /d/myProject (main)
$ git log --graph --oneline -3 >x.txt
29447@GW64 /d/myProject (main)
$ git add x.txt
warning: in the working copy of 'x.txt', LF will be replaced by CRLF the next time Git touches it
29447@GW64 /d/myProject (main)
$ git commit -m "main add x.txt"
[main b782420] main add x.txt
1 file changed, 3 insertions(+)
create mode 100644 x.txt
29447@GW64 /d/myProject (main)
$ git log --graph --oneline -3 >y.txt
29447@GW64 /d/myProject (main)
$ git add y.txt
warning: in the working copy of 'y.txt', LF will be replaced by CRLF the next time Git touches it
29447@GW64 /d/myProject (main)
$ git commit -m "main add y.txt"
[main 34730f6] main add y.txt
1 file changed, 3 insertions(+)
create mode 100644 x.txt
# step4:merge合并分支(test_branch合并到main)
29447@GW64 /d/myProject (main)
$ git merge test_branch -m "Merge branch 'test_branch'"
Merge made by the 'ort' strategy.
c | 2 ++
d.txt | 2 ++
2 files changed, 4 insertions(+)
create mode 100644 c
create mode 100644 d.txt
29447@GW64 /d/myProject (main)
$ git log test_branch --graph --oneline -6
* b581603 (test_branch) test_branch add d
* 409dc16 test_branch add c
* 989cbae (origin/main, origin/HEAD) delete patch files
* dc06cd4 (tag: v2.0, origin/test_branch, dev_branch) test patch again
* 848e4d9 test patch again
* de2aaa1 update function statement
# step5:main分支中,test_branch先提交的记录,亦放在HEAD
29447@GW64 /d/myProject (main)
$ git log main --graph --oneline -6
* 7d90224 (HEAD -> main) Merge branch 'test_branch'
|\
| * b581603 (test_branch) test_branch add d
| * 409dc16 test_branch add c
* | 34730f6 main add y.txt
* | b782420 main add x.txt
|/
* 989cbae (origin/main, origin/HEAD) delete patch files
- rebase
29447@GW64 /d/myProject (test_branch)
$ git commit -am "test_branch:delete upcase"
[test_branch c7fb399] test_branch:delete upcase
3 files changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 E
delete mode 100644 F
delete mode 100644 touch
29447@GW64 /d/myProject (test_branch)
$ git checkout main
Switched to branch 'main'
29447@GW64 /d/myProject (main)
$ git commit -am "main:delete lowcase 16:18"
[main 90cee55] main:delete lowcase 16:18
6 files changed, 10 deletions(-)
delete mode 100644 W
delete mode 100644 Z
delete mode 100644 c
delete mode 100644 d.txt
delete mode 100644 x.txt
delete mode 100644 y.txt
29447@GW64 /d/myProject (main)
$ git rebase test_branch # 第2次重置分支;从共同分支开始,test_branch在下,main在上(即标为HEAD)
Successfully rebased and updated refs/heads/main.
29447@GW64 /d/myProject (main)
$ git log --graph --oneline -8
* 4104926 (HEAD -> main) main:delete lowcase 16:18
* 99b4dda test_branch add W 13:36
* bb450ee test_branch add Z 13:35
* 0c5ae5f main add y.txt
* 54ff117 main add x.txt
* c7fb399 (test_branch) test_branch:delete upcase
* 32227be test_branch add F 13:34
* e2fac33 test_branch add E 13:33
29447@GW64 /d/myProject (main)
$ git checkout test_branch
Switched to branch 'test_branch'
Your branch is ahead of 'origin/test_branch' by 6 commits.
(use "git push" to publish your local commits)
29447@GW64 /d/myProject (test_branch)
$ git commit -am "test_branch:delete c&d 16:22"
[test_branch b3c1508] test_branch:delete c&d 16:22
2 files changed, 4 deletions(-)
delete mode 100644 c
delete mode 100644 d.txt
29447@GW64 /d/myProject (test_branch)
$ git checkout main
Switched to branch 'main'
29447@GW64 /d/myProject (main)
$ git rebase test_branch # 重置分支,第3次
Successfully rebased and updated refs/heads/main.
29447@GW64 /d/myProject (main)
$ git log --graph --oneline -20
* 8b58b4c (HEAD -> main) main:delete lowcase 16:18 # main分支5次commit;test_branch分支提交后,原HEAD仍指向main
* e12b92a test_branch add W 13:36
* 5c27797 test_branch add Z 13:35
* 3f51b07 main add y.txt
* 36b7679 main add x.txt
* b3c1508 (test_branch) test_branch:delete c&d 16:22 # test_branch分支5次commit
* c7fb399 test_branch:delete upcase
* 32227be test_branch add F 13:34
* e2fac33 test_branch add E 13:33
* b581603 test_branch add d
* 409dc16 test_branch add c
* 989cbae (origin/main, origin/HEAD) delete patch files # 远程仓库
* dc06cd4 (tag: v2.0, origin/test_branch, dev_branch) test patch again
Git rebase操作时,某些提交并没有显示出来,无法进行合并
29447@GW64 /d/myProject (main) $ git rebase temp_branch dropping eb3905b33ce06c2158e6459e2d38a39ada58ea24 main:delete stash_file -- patch contents already upstream Successfully rebased and updated refs/heads/main.
git rebase --interactive HEAD~3
# 手动操作所有需要合并的提交
git branch --contains <commit-hash>
# 检查提交是否已经并合并到其他分支
$ git branch --contains c68e6db
* main
temp_branch
$ git branch --contains ebd0f75 # 原有提交被rebase删除