使用 git rebase --onto 無痛更新分支

提交 PR 前需要更新 feature branch 的 base branch,這時候就可以使用 rebase --onto 來無痛更新分支,直接將 feature branch 的 commit 接到 main branch 之後。

首先,我們有 main 和 feature 兩個分支,main 分支上有 M1 和 M2 兩個 commit,feature 分支上有 A1 和 A2 兩個 commit。其中,feature 分支是基於 main 分支 commit 702694b 建立 (branch out)。這個 commit ID 是必須要記住的,稍等會用到。

使用 `git rebase --onto` 無痛更新分支

這時候希望把 feature 分支上的 A1 和 A2 兩個 commit 接到 main 分支的 M2 之後,就可以使用 rebase --onto 來無痛更新分支。

步驟:

  1. 切換到 main 分支。
  2. 更新到最新的 main 分支。
    git checkout main
    git pull origin main
    
  3. 切換到 feature 分支。
  4. 使用 git rebase --onto 來更新分支。
    git checkout feature
    git rebase --onto main 702694b
    
  5. 修復衝突、繼續 rebase。在解決衝突時,依據 codebase 的狀況來決定要如何處理,可以使用 git add <file-path>git add . 加入要變更檔案,然後使用 git rebase --continue 來繼續 rebase。
  6. 推送分支 (git push origin feature -f)。

使用 `git rebase --onto` 無痛更新分支

推上遠端之後,在 GitHub 上看到 feature 分支的 commit 已經接到 main 分支的 M2 之後。

使用 `git rebase --onto` 無痛更新分支

若持續開發,main 分支上有新加入的 M3 commit,feature 分支上也有新加入的 A3 commit,要再次更新 feature 分支至最新的 main 分支,也就是把 A1、A2、A3 接到 M3 之後,只需要重複上述步驟即可。同樣也是使用 git rebase --onto 來無痛更新分支,基準點仍是 feature 的 branch out 的 commit ID 702694b

使用 `git rebase --onto` 無痛更新分支

git checkout main
git pull origin main

git checkout feature
git rebase --onto main 702694b

再次推上遠端,在 GitHub 上看到 feature 分支的 commit 已經接到 main 分支的 M3 之後。

使用 `git rebase --onto` 無痛更新分支

這樣就可以無痛更新分支,並且保持 commit history 的整潔。

使用 `git rebase --onto` 無痛更新分支

結論


git git rebase git merge git cherry-pick GitHub pull request