Git rebase --onto
指令可把某一段連續的 commits rebase 到新的 parent commit 上.
我通常會用這個方式捨棄當前 branch 和目標 branch 重複但不完全相同的 commits, 避免直接 rebase 時遇到 conflict.
git rebase NEW_PARENT_COMMIT
git rebase NEW_PARENT_COMMIT BRANCH
git checkout BRANCH_CONTAINS_COMMITS_TO_BE_REBASED
或是 checkout 要被 rebase 的連續 commits 的最後一個 commit
git checkout LAST_COMMIT_OF_COMMITS_TO_BE_REBASED
git rebase --onto NEW_PARENT_COMMIT PARENT_OF_REACHABLE_COMMIT
git rebase --onto NEW_PARENT_COMMIT PARENT_OF_FIRST_OF_REACHABLE_COMMITS LAST_OF_REACHABLE_COMMITS
git rebase --onto NEW_PARENT_COMMIT PARENT_OF_FIRST_OF_REACHABLE_COMMITS BRANCH_NAME
效果等同如下指令.
git checkout BRANCH_NAME
git rebase --onto NEW_PARENT_COMMIT PARENT_OF_FIRST_OF_REACHABLE_COMMITS
Syntax
# Rebase a series of commits
git checkout BRANCH_NAME or REBASE_UNTIL_COMMI
git rebase --onto NEW_PARENT_COMMIT OLD_PARRENT_COMMIT <REBASE_UNTIL_COMMIT>
# Rebase a part of commits of a branch
git checkout BRANCH_NAME
git rebase --onto NEW_PARENT_COMMIT OLD_PARRENT_COMMIT
Git
Written on
March
28th,
2022
by
Borting