首页 iOS.& Swift Books 高级git

9
功能分支工作流程 由Jawwad Ahmad撰写

在上一章中,您学习了如何使用集中式工作流程直接在主分支上工作,这在某些情况下方便。

但是,大多数时候,您将使用一些版本 功能分支工作流程。在开始新功能之前,您将从Master创建一个分支并在其上工作。完成后,您将将功能分支合并回主。

创建功能分支基本上为您提供了您自己的Master Branch的冻结版本。它还允许您延迟推送您的提交到掌握,直到您的功能完成,这使主分支为每个人的稳定状态。

在以前的章节中,您学习了如何创建分支机构,rebase分支,解析冲突并将分支合并回主。

在本章中,您将学习如何在团队设置中有效地使用这些技术 - 即,当多个开发人员正在处理分支机构时,它们将会定期合并到主站。

您还将学习Rebasing和合并周围的最佳实践,并将沿途播放一些提示和技巧。

当用户使用该功能科的工作流程

有一些有限的场景,集中式工作流程是一个很好的契合。在所有其他情况下,您将使用某种形式的功能分支工作流。

功能分支工作流程是所有其他GIT工作流程的基础,如Gitflow和分叉工作流程。

根据您的团队的需求,您可以选择使用此工作流程的简单版本,或者您可以决定采用其他要求,例如指定开发人员需要命名为特定方式或使用特定的前缀。

以下是一些方案,您当然需要使用功能分支工作流程。

当开发并行功能

在团队中工作时,等待在另一个开发人员在另一个开发人员开始之前完成工作往往是不可行的。开发人员需要在同一代码库中并行工作多个功能。

当您的代码需要审核时

无论团队规模或您立即工作的功能如何,如果您需要其他开发人员查看代码,则必须使用功能分支。

当发展还共享代码

功能分支允许您在将其合并到Master之前共享代码。例如,您可能需要代码,即另一个开发人员目前正在处理的代码,因此尚未在Master中使用。在此方案中,您可以从具有所需代码的另一个分支创建分支。

在一个特征上合作时

分支机构允许您在新功能上与其他开发人员合作。多个开发人员可以在共享分支上工作,然后在完成该功能时将该分支合并到开发中。

入门

如前一章,您将通过扮演不同开发人员的角色来模拟团队的工作。但是,在本章中将在此章节中切换角色。

starter
└── repos
    ├── alex
    │   └── checklists
    ├── beth
    │   └── checklists
    ├── chad
    │   └── checklists
    └── checklists.git
cd path/to/starter/repos/alex/checklists  # 1st Tab
cd path/to/starter/repos/beth/checklists  # 2nd Tab
cd path/to/starter/repos/chad/checklists  # 3rd Tab
cd path/to/starter/repos/checklists.git   # 4th Tab

初始项目状态

这支球队在两个特征分支机构中努力工作。亚历克斯一直在努力 Alex功能,而贝丝和乍得一直在一个名为的共享分支中共同努力 Beth-Chad-Feature.

遥控器位于遥控器上,虚线节点是本地提交
Tahuc Gobey Iva Ox Gjo Viwoca,Hanbes Xexux Avu Cejum dehcabz

git log --oneline --graph --all
* b2deca5 (beth-chad-feature) C5: Added <footer> to <body>
* 4fbfda4 B5: Moved <h1> and <p> within <header>
| * 51bdc3c (HEAD -> master) C4: Updated section styling to u...
| * 6a52517 C3: Added "介绍" section
| * fcb3dbc C2: Added background-color css for section
|/
* 6bc53bb C1: Added "Morning Routine Checklist" section
...

项目路线图

如前所述,您将在本章中更加切换角色 - 不仅仅是因为乍得加入了团队,还因为还有更多要做。 :]

与主控更新分支的重要性

使用Master中的新代码更新您的分支有两种主要原因。

如何更新与主分支

有两种方法可以使用Master更新分支机构:您可以将分支绑定到Master上,或者您可以将主分支合并到本地分支中。

更新两个项目分支机构

自从 Alex功能 尚未被推到原产地,你会纠正它 掌握。和以来 Beth-Chad-Feature 已被推动到原产地,贝丝和乍得分享它,你会合并 掌握 into it instead.

git log --oneline --graph --all
* b2deca5 (origin/beth-chad-feature) C5: Added <footer> to <b...
* 4fbfda4 B5: Moved <h1> and <p> within <header>
| * 9f06a73 (HEAD -> alex-feature) A6: Added "Evening Routine...
| * 427b5ee A5: Added h2 color to style.css
| | * 51bdc3c (origin/master, origin/HEAD, master) C4: Update...
| | * 6a52517 C3: Added "介绍" section
| |/
| * fcb3dbc C2: Added background-color css for section
|/
* 6bc53bb C1: Added "Morning Routine Checklist" section
...
git log --oneline --graph alex-feature master
* 9f06a73 (HEAD -> alex-feature) A6: Added "Evening Routine C...
* 427b5ee A5: Added h2 color to style.css
| * 51bdc3c (origin/master, origin/HEAD, master) C4: Updated ...
| * 6a52517 C3: Added "介绍" section
|/
* fcb3dbc C2: Added background-color css for section
* 6bc53bb C1: Added "Morning Routine Checklist" section
...
亚历克斯 - 功能分支的当前位置
Mohjogd cuwoqauz ED KWI elus,fuimupa zbewcz

git rebase master
运行后Alex-Feats的位置:Git Rebase Master
dajasuoq er adam-xoolofo avvug zuvxomr:noq gepiqo sefqij

git log --oneline --graph alex-feature master
* 28d0ae5 (HEAD -> alex-feature) A6: Added "Evening Routine C...
* b0f7244 A5: Added h2 color to style.css
* 51bdc3c (origin/master, origin/HEAD, master) C4: Updated se...
* 6a52517 C3: Added "介绍" section
* fcb3dbc C2: Added background-color css for section
* 6bc53bb C1: Added "Morning Routine Checklist" section
...
git log --oneline --graph --all
* 19f8c99 (HEAD -> beth-chad-feature) B6: Added <hr/> in <hea...
* b2deca5 (origin/beth-chad-feature) C5: Added <footer> to <b...
* 4fbfda4 B5: Moved <h1> and <p> within <header>
| * 51bdc3c (origin/master, origin/HEAD, master) C4: Updated ...
| * 6a52517 C3: Added "介绍" section
| * fcb3dbc C2: Added background-color css for section
|/
* 6bc53bb C1: Added "Morning Routine Checklist" section
...
贝丝本地存储库的状态
Sluge Oy Kuqd'z Bagob Qevopetakp

git merge master
git log --oneline --graph --all
*   fbffe40 (HEAD -> beth-chad-feature) Merge branch 'master'...
|\
| * 51bdc3c (origin/master, origin/HEAD, master) C4: Updated ...
| * 6a52517 C3: Added "介绍" section
| * fcb3dbc C2: Added background-color css for section
* | 19f8c99 B6: Added <hr/> in <header>
* | b2deca5 (origin/beth-chad-feature) C5: Added <footer> to ...
* | 4fbfda4 B5: Moved <h1> and <p> within <header>
|/
* 6bc53bb C1: Added "Morning Routine Checklist" section
...
合并掌握到伯乍得 - 功能之后
Imgey valbacq jogxoh uffu kikk-nwiq-daexane

git push
推伯查德 - 功能之后起源
Ovfam wollijd胶水-whel-kauluje双向iyoxig

git log --oneline --graph --all
* 347bcd3 (HEAD -> beth-chad-feature) C6: Removed "Routine" f...
* b2deca5 (origin/beth-chad-feature) C5: Added <footer> to <b...
* 4fbfda4 B5: Moved <h1> and <p> within <header>
| * 51bdc3c (origin/master, origin/HEAD, master) C4: Updated ...
| * 6a52517 C3: Added "介绍" section
| * fcb3dbc C2: Added background-color css for section
|/
* 6bc53bb C1: Added "Morning Routine Checklist" section
...

git fetch
git log --oneline --graph --all
*   fbffe40 (origin/beth-chad-feature) Merge branch 'master' ...
|\
| * 51bdc3c (origin/master, origin/HEAD, master) C4: Updated ...
| * 6a52517 C3: Added "介绍" section
| * fcb3dbc C2: Added background-color css for section
* | 19f8c99 B6: Added <hr/> in <header>
| | * 347bcd3 (HEAD -> beth-chad-feature) C6: Removed "Routin...
| |/
|/|
* | b2deca5 C5: Added <footer> to <body>
* | 4fbfda4 B5: Moved <h1> and <p> within <header>
|/
* 6bc53bb C1: Added "Morning Routine Checklist" section
...
获取Beth更新后乍得本地存储库的状态
CCUDI EC HRUM'C FEPEG YOSANUTAYQ AJROY FUHMYIFM WESG'H UNKUQEH

git pull --rebase
...
CONFLICT (content): Merge conflict in index.html
error: Failed to merge in the changes.
Patch failed at 0001 C6: Removed "Routine" from heading
hint: Use 'git am --show-current-patch' to see the failed patch
...
git am --show-current-patch
C6: Removed "Routine" from heading
...
       <section>
-        <h2>Morning Routine Checklist</h2>
+        <h2>Morning Checklist</h2>
       </section>
...
16     <main>
17 <<<<<<< HEAD
18       <section class="intro-section">
19         <h2>Introduction</h2>
20       </section>
21
22       <section class="checklist-section">
23         <h2>Morning Routine Checklist</h2>
24 =======
25       <section>
26         <h2>Morning Checklist</h2>
27 >>>>>>> C6: Remove "Routine" from headings
28       </section>
29     </main>
16     <main>
17       <section class="intro-section">
18         <h2>Introduction</h2>
19       </section>
20
21       <section class="checklist-section">
22         <h2>Morning Checklist</h2>
23       </section>
24     </main>
git add index.html
git rebase --continue
git show
C6: Removed "Routine" from heading
...
       <section class="checklist-section">
-        <h2>Morning Routine Checklist</h2>
+        <h2>Morning Checklist</h2>
       </section>
...
git push
重建和推动后乍得的存储库状态
Rpayi er Tfad'r Teyavosagk Onpak Curikuxg Avz甲蛋白

将分支机构合并到掌柜中

Alex is ready to push his branch up for review. He’ll do a final git fetch to see if anyone has pushed additional updates to 掌握 他将需要重新划伤。

git fetch
...
From ../../checklists
 b2deca5..8aad97e  beth-chad-feature -> origin/beth-chad-feature
git log --oneline --graph alex-feature master
* 28d0ae5 (HEAD -> alex-feature) A6: Added "Evening Routine C...
* b0f7244 A5: Added h2 color to style.css
* 51bdc3c (origin/master, origin/HEAD, master) C4: Updated se...
* 6a52517 C3: Added "介绍" section
* fcb3dbc C2: Added background-color css for section
* 6bc53bb C1: Added "Morning Routine Checklist" section
open index.html
index.html的关于重订基亚历克斯特性分支
aqqof.thfm UC QSI zaxalob ewew,meexita npachq

git show master
C4: Updated section styling to use a class
...
diff --git a/index.html b/index.html
     <main>
-      <section>
+      <section class="intro-section">
         <h2>Introduction</h2>
       </section>

-      <section>
+      <section class="checklist-section">
         <h2>Morning Routine Checklist</h2>
...
diff --git a/style.css b/style.css
...
-section {
+.checklist-section {
     background-color: lightcyan;
 }
+
+.intro-section {
+    background-color: lavender;
+}
-      <section>
+      <section class="checklist-section">
         <h2>Evening Routine Checklist</h2>
git add index.html
git commit --amend --no-edit
git show
       </section>
+
+      <section class="checklist-section">
+        <h2>Evening Routine Checklist</h2>
+      </section>
     </main>
open index.html
index.html在Rebased Alex-Feator分支与修正的提交
imbov.wlpv ax lza yaminak uluc-deucize yyevsl buxw fre asuckex nifkig

git push -u origin head
git push --set-upstream origin alex-feature  # same as above
...
To ../../checklists.git
 * [new branch]      head -> alex-feature
Branch 'alex-feature' set up to track remote branch 'alex-
feature' from 'origin'.
git fetch
...
From ../../checklists
   fbffe40..8aad97e  beth-chad-feature -> origin/beth-chad-fe...
 * [new branch]      alex-feature      -> origin/alex-feature
git pull
git checkout alex-feature
git log -p master..
git checkout master
git merge alex-feature
git push
git branch -d alex-feature
git push origin --delete alex-feature
open index.html
的index.html在伯查德 - 特性分支
oxhev.pmff LME小车 - ceky-wvos-luenawu bguzgv

       <section class="checklist-section">
-        <h2>Morning Routine Checklist</h2>
+        <h2>Morning Checklist</h2>
       </section>
git fetch
...
From ../../checklists
   51bdc3c..cca927c  master     -> origin/master

git checkout master
git pull
git checkout -
Switched to branch 'beth-chad-feature'
git merge master --no-edit
open index.html
乍得的index.html在将大师合并到beth-chad-shared之后
Rziw'l ikhug.vzvm Ugcuv Honhaty Luzquc Imtivd-Hfag-Kbelic

       <section class="checklist-section">
-        <h2>Evening Routine Checklist</h2>
+        <h2>Evening Checklist</h2>
       </section>
git commit -am "C7: Removed Routine from heading"
open index.html

git push
git fetch
...
From ../../checklists
 8aad97e..cde0da3  beth-chad-feature -> origin/beth-chad-feature
 51bdc3c..cca927c  master            -> origin/master
git checkout master
git pull
git checkout beth-chad-feature

git checkout beth-chad-feature
git rebase master
First, rewinding head to replay your work on top of it...
Applying: B5: Moved <h1> and <p> within <header>
...
Applying: C5: Added <footer> to <body>
...
Applying: B6: Added <hr/> in <header>
Applying: C6: Removed "Routine" from heading
..
Applying: C7: Removed Routine from heading
git log -p -2
C7: Removed Routine from heading
...
       <section class="checklist-section">
-        <h2>Evening Routine Checklist</h2>
+        <h2>Evening Checklist</h2>
       </section>
...

C6: Removed "Routine" from heading
...
       <section class="checklist-section">
-        <h2>Morning Routine Checklist</h2>
+        <h2>Morning Checklist</h2>
       </section>
...
git rebase -i head~2
pick 0383368 C6: Removed "Routine" from heading
pick 4652fdc C7: Removed Routine from heading
pick 0383368 C6: Removed "Routine" from heading
f 4652fdc C7: Removed Routine from heading
git show
C6: Removed "Routine" from heading
...
       <section class="checklist-section">
-        <h2>Morning Routine Checklist</h2>
+        <h2>Morning Checklist</h2>
       </section>

       <section class="checklist-section">
-        <h2>Evening Routine Checklist</h2>
+        <h2>Evening Checklist</h2>
       </section>
...
git checkout master
git merge beth-chad-feature
git push
git branch -d beth-chad-feature
warning: not deleting branch 'beth-chad-feature' that is not yet
merged to 'refs/remotes/origin/beth-chad-feature', even though
it is merged to HEAD.
error: The branch 'beth-chad-feature' is not fully merged.
If you are sure you want to delete it, run 'git branch -D beth-chad-feature'.
git push origin --delete beth-chad-feature
git branch -d beth-chad-feature

关键点

  • 在“功能分支工作流程”中,您可以随时创建一个分支,您要在新功能上工作。
  • 您应该在主机中定期更新您的分支。
  • 在推送审核之前,您必须使用Master更新您的分支。
  • 您可以通过将分支重建到Master或通过将主设备合并到分支中来包含Master的更改。
  • 如果您正在进行本地或私人分支机构,最好将您的分支绑定到掌柜上。
  • 永远不要重建公共分支;相反,将主站合并到分支中以防止重写历史记录。

有技术问题吗?要报告bug? 您可以向官方书籍论坛中的书籍作者提出问题和报告错误 这里.

有反馈分享在线阅读体验吗? 如果您有关于UI,UX,突出显示或我们在线阅读器的其他功能的反馈,您可以将其发送到设计团队,其中表格如下所示:

© 2021 Razeware LLC

您可以免费读取,本章的部分显示为 混淆了 文本。解锁这本书,以及我们整个书籍和视频目录,带有Raywenderlich.com的专业订阅。

现在解锁

要突出或记笔记,您需要在订阅中拥有这本书或自行购买。