freeCodeCamp/guide/chinese/git/git-rebase/index.md

3.5 KiB
Raw Blame History

title localeTitle
Git Rebase 去Rebase

Git Rebase

在Git中重新分支分支是一种将整个分支移动到树中另一个点的方法。最简单的例子是在树中进一步向上移动分支。假设我们在A点有一个与主分支不同的分支

        /o-----o---o--o-----o--------- branch 
 --oo--A--o---o---o---o----o--ooo--- master 

如果你改变,你可以这样移动:

                                   /o-----o---o--o-----o------ branch 
 --oo--A--o---o---o---o----o--ooo master 

要进行rebase请确保在master分支中的rebase中拥有所需的所有提交。检查要重新绑定的分支并键入git rebase master 其中master是要重新绑定的分支

也可以在不同的分支上进行rebase以便例如基于另一个分支的分支让我们称之为功能在master上重新定位

                            /---oo branch 
           /---oooo---o--o------ feature 
 ----o--ooA----o---o--ooo--o--o- master 

在你签出分支后,在git rebase master branchgit rebase master ,你会得到:

           /---oooo---o--o------ feature 
 ----o--ooA----o---o--ooo--o--o- master 
                                  \---oo branch 

在控制台中使用Git rebase交互

要在控制台中使用带有提交列表的git rebase 您可以选择编辑或删除rebase

  • 输入git rebase -i HEAD~5 ,最后一个数字是您要查看的最近一次提交的任意数量的提交。
  • 在vim中esc ,然后i开始编辑测试。
  • 在左侧,您可以使用以下命令之一覆盖pick 。如果要将提交压缩到前一个提交并丢弃提交消息,请在提交pick位置输入f
pick 452b159 <message for this commit> 
 pick 7fd4192 <message for this commit> 
 pick c1af3e5 <message for this commit> 
 pick 5f5e8d3 <message for this commit> 
 pick 5186a9f <message for this commit> 
 
 # Rebase 0617e63..5186a9f onto 0617e63 (30 commands) 
 # 
 # Commands: 
 # p, pick = use commit 
 # r, reword = use commit, but edit the commit message 
 # e, edit = use commit, but stop for amending 
 # s, squash = use commit, but meld into previous commit 
 # f, fixup = like "squash", but discard this commit's log message 
 # x, exec = run command (the rest of the line) using shell 
 # d, drop = remove commit 
 # 
 # These lines can be re-ordered; they are executed from top to bottom. 
 # 
 # If you remove a line here THAT COMMIT WILL BE LOST. 
 # 
 # However, if you remove everything, the rebase will be aborted. 
 # 
 # Note that empty commits are commented out 
  • 输入esc然后输入:wq进行保存并退出。
  • 如果它成功重新绑定,那么你需要强制推送你的更改,使用git push -f将更新的版本添加到你的github仓库。
  • 如果存在合并冲突,可通过多种方法解决此问题,包括遵循本指南中的建议。一种方法是在文本编辑器中打开文件并删除不需要的代码部分。然后使用git add <file name>然后使用git rebase --continue 。您可以通过输入git rebase --skip跳过冲突的提交,在控制台中输入git rebase --abort退出git rebase。

更多信息: