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,我们可以清理我们的历史树,正如本指南前面所讨论的

让我们从取消之前的合并开始。

$ 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