git rebase 举例

介绍

前面看到的 git merge 保持了 main / feature_1 原始节点的一致性,代价就是多了一个 merge 节点

那是否有方法可以不要有这些 merge 看起来"无用"的节点呢?那就用到了 rebase 方案

操作步骤

bash 复制代码
$ git checkout feature_1b
Switched to branch 'feature_1b'
Your branch is up to date with 'origin/feature_1b'.
$ 
$ git log
commit 33063bff55e8d7142a2210a6973455abc98ec431 (HEAD -> feature_1b, origin/feature_1b)
Author: powertest
Date:   Tue Feb 3 08:40:22 2026 +0000

    Initial commit
$ touch feature_1b.c
$ echo feature_1b > feature_1b.c 
$ git status
On branch feature_1b
Your branch is up to date with 'origin/feature_1b'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	feature_1b.c

nothing added to commit but untracked files present (use "git add" to track)
$ git add .
$ git commit -m "Add feature_1b.c"
[feature_1b 8c2dda5] Add feature_1b.c
 1 file changed, 1 insertion(+)
 create mode 100644 feature_1b.c
$ git push
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 298 bytes | 298.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: 
remote: To create a merge request for feature_1b, visit:
remote:   http://gitlab.powertest.com.cn/powertest/test-git-feature-1/-/merge_requests/new?merge_request%5Bsource_branch%5D=feature_1b
remote: 
To ssh://gitlab.powertest.com.cn:12022/powertest/test-git-feature-1.git
   33063bf..8c2dda5  feature_1b -> feature_1b
$ git log
commit 8c2dda57b712347a8108e5a4278a4bbbff54fb52 (HEAD -> feature_1b, origin/feature_1b)
Author: powertest
Date:   Tue Feb 3 18:02:15 2026 +0800

    Add feature_1b.c

commit 33063bff55e8d7142a2210a6973455abc98ec431
Author: powertest
Date:   Tue Feb 3 08:40:22 2026 +0000

    Initial commit
$ git log  --oneline --graph
* 8c2dda5 (HEAD -> feature_1b, origin/feature_1b) Add feature_1b.c
* 33063bf Initial commit
$ git rebase main
Successfully rebased and updated refs/heads/feature_1b.
$ git status
On branch feature_1b
Your branch and 'origin/feature_1b' have diverged,
and have 2 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

nothing to commit, working tree clean
$ git log
commit 0b8b109c1d9d5df04baddb68b6493c81e5665823 (HEAD -> feature_1b)
Author: powertest
Date:   Tue Feb 3 18:02:15 2026 +0800

    Add feature_1b.c

commit a079cfa408b94e871796efc799a58b7f6ad011f1 (origin/main, origin/HEAD, main)
Author: powertest
Date:   Tue Feb 3 17:09:22 2026 +0800

    Add main.c

commit 33063bff55e8d7142a2210a6973455abc98ec431
Author: powertest
Date:   Tue Feb 3 08:40:22 2026 +0000

    Initial commit
$ git rebase --continue
fatal: No rebase in progress?
$ git log  --oneline --graph
* 0b8b109 (HEAD -> feature_1b) Add feature_1b.c
* a079cfa (origin/main, origin/HEAD, main) Add main.c
* 33063bf Initial commit
$ git push
To ssh://gitlab.powertest.com.cn:12022/powertest/test-git-feature-1.git
 ! [rejected]        feature_1b -> feature_1b (non-fast-forward)
error: failed to push some refs to 'ssh://gitlab.powertest.com.cn:12022/powertest/test-git-feature-1.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

$ git push --force-with-lease origin feature_1b
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 333 bytes | 333.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: 
remote: To create a merge request for feature_1b, visit:
remote:   http://gitlab.powertest.com.cn/powertest/test-git-feature-1/-/merge_requests/new?merge_request%5Bsource_branch%5D=feature_1b
remote: 
To ssh://gitlab.powertest.com.cn:12022/powertest/test-git-feature-1.git
 + 8c2dda5...0b8b109 feature_1b -> feature_1b (forced update)
$ git log
commit 0b8b109c1d9d5df04baddb68b6493c81e5665823 (HEAD -> feature_1b, origin/feature_1b)
Author: powertest
Date:   Tue Feb 3 18:02:15 2026 +0800

    Add feature_1b.c

commit a079cfa408b94e871796efc799a58b7f6ad011f1 (origin/main, origin/HEAD, main)
Author: powertest
Date:   Tue Feb 3 17:09:22 2026 +0800

    Add main.c

commit 33063bff55e8d7142a2210a6973455abc98ec431
Author: powertest
Date:   Tue Feb 3 08:40:22 2026 +0000

    Initial commit

优缺点比较

通过上面我们可以看出,rebase 方案的 rebase 的含义,将整个记录从树型结构变成了线型结构,代价就是部分节点的值发生了变化。如果分支别人正在并行开发,将会给别人造成很大的问题。

所以个人研发分支不跟别人共用的化可以使用 rebase 方案,共用分支的修改用 merge

相关推荐
无限进步_4 小时前
21. 合并两个有序链表 - 题解与详细分析
c语言·开发语言·数据结构·git·链表·github·visual studio
宁静致远20214 小时前
STM32CubeMX、MDK(Keil MDK)、git、vscode等工具中统一编码设置(UTF-8),确保中文支持,避免乱码问题
git·vscode·stm32
love530love13 小时前
Windows 多 Git 环境冲突:一个环境变量优先级引发的血案
人工智能·windows·git·环境变量·scoop
无限进步_16 小时前
面试题 02.04. 分割链表 - 题解与详细分析
c语言·开发语言·数据结构·git·链表·github·visual studio
甲枫叶1 天前
【claude】Claude Code正式引入Git Worktree原生支持:Agent全面实现并行独立工作
java·人工智能·git·python·ai编程
0xwang1 天前
Deepin25社区版修改Git编码
git
第七序章1 天前
【Linux学习笔记】git三板斧
linux·运维·服务器·笔记·git·学习
三无少女指南1 天前
开发者环境配置最佳实践:编辑器Cursor ,VS Code的上位体验实现 AI 与 WSL 联动
运维·c语言·数据库·windows·git·编辑器
Rhystt2 天前
furryCTF 题解|Web方向|保姆级详解|固若金汤、DeepSleep
git·python·安全·web安全·网络安全
头发那是一根不剩了2 天前
Git 常用命令
git