Skip to main content
  1. Learn center
  2. Software Development
  3. Guides
  4. Git tutorial
  5. 如何使用 Git
  6. 如何在 Git 中使用分支
  7. 將分支變基
GuidesSoftware DevelopmentBacklog
Git

Project and code management together.

將分支變基

另一種將issue3分支整合到主分支的方法是 git rebase 指令。使用 rebase,我們可以清理我們的 history tree,正如本指南前面所討論的

讓我們從取消之前的合併開始。

$ git reset --hard HEAD~

這就是我們的歷史紀錄現在的樣子:

History before rebase

接下來,切換到issue3分支,並變基到主分支。

$ git checkout issue3
Switched to branch 'issue3'
$ git rebase main
Auto-merging sample.txt
CONFLICT (content): Merge conflict in sample.txt
error: could not apply 470d684... My Commit
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply 470d684... My Commit

當 rebase 過程中發生衝突時,您必須解決它才能恢復操作。

Anyone can learn Git with this tutorial and Backlog
add: Register a change in an index
<<<<<<< HEAD
commit: Save the status of an index
=======
pull: Obtain the content of a remote repository
>>>>>>> issue3

衝突解決後,您可以使用--continue選項恢復變基。

在套用您的更改之前,Git 將開啟您預設的文本編輯器 (大多數 Unix 系統上的 Vim 或 Emacs),讓您有機會編輯 rebase 訊息。如果您想退出,並回滾 rebase 操作,您可以使用--abort選項。

$ git add myfile.txt
$ git rebase --continue
Applying: append description of the pull command

現在,我們的歷史紀錄看起來像這樣:

Current history

隨著issue3分支變基到main,我們現在可以進行快轉合併。

切換到主分支,並將issue3main合併。

$ git checkout main
Switched to branch 'main'
$ git merge issue3
Updating 8f7aa27..96a0ff0
Fast-forward
myfile.txt |    1 +
  1 files changed, 1 insertions(+), 0 deletions(-)

myfile.txt檔案的內容現在將與之前合併中的內容相同。歷史紀錄現在將如下所示:

Current history

Subscribe to our newsletter

Learn with Nulab to bring your best ideas to life