首页·终端命令

git rebase

git

用法

git rebase [-i] [--onto <base>] <upstream>

把你的提交重新放到另一个基点上;哈希会变,所以别对别人已经拉走的分支做 rebase,那种情况用 merge。

常用选项

选项含义
-iinteractive: reorder, squash, reword, drop commits
--onto <base>move the range onto a different base
--continuecarry on after fixing a conflict
--abortput the branch back exactly as it was
--skipdrop the commit that will not apply
--autostashstash uncommitted work and put it back afterwards
--autosquashapply fixup!/squash! commits automatically

示例

git rebase main

replays your commits on top of main, giving them new hashes

git rebase -i HEAD~3

squash or reword the last three commits

git rebase --abort

cancel a rebase and return to the original branch

难的地方在于撤销有好几种:把分支的指向挪走,和补一个反向提交,不是一回事。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. git rebase 是做什么的?

把你的提交重新放到另一个基点上;哈希会变,所以别对别人已经拉走的分支做 rebase,那种情况用 merge。

Q. 怎么写?

git rebase [-i] [--onto <base>] <upstream> —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 7 个,完整列表在 man git。这条命令属于git。

相关命令

man 手册: man git-rebase