iT邦幫忙

2021 iThome 鐵人賽

DAY 5
0
自我挑戰組

DevOps的下克上之旅( ° ∀ ° )ノ゙系列 第 5

Day 5 : Git 多人協作

開分支

具體上要開那些branch呢? 我習慣大致簡化分成三種

  • master 上到production的版本,沒把握不要改動他
  • develop 開發版,開發過程的主要幹線,讓feature去修改他
  • feature/人 團隊的個人開發,開發一個段落merge到develop版

https://ithelp.ithome.com.tw/upload/images/20210906/20119044vgU4DIm7iA.png

如上圖,團隊主要開發分支會在develop上。每天早晨主管核對完每個人的任務後,每個人會各自開發她們的feature,如人物(b1)開發feature/b1,人物(b2)開發feature/b2。完成自己的任務,或是快下班了,再將今天開發的東西merge給develop,當develop開發成熟後,就可以將develop merge給master,完成這一次的版本更新。

詳細參考: https://nvie.com/posts/a-successful-git-branching-model/

Merge

Feature開發完成後要併回develop,首先我們要先知道是誰合併誰。merge合併就像是吸收的動作,git status看一下現在處於哪個分支,並且要吸收掉哪個其他分支。

# 想要develop merge掉feature/b1
# 先切換到develop
git checkout develop

# 合併
git merge --no-ff feature/b1

# --no-ff是合併後分支保留的意思

如果沒有加上--no-ff默認會把被合併的分支消滅,所以加上讓feature/b1繼續走下去。如下圖,成功合併。
https://ithelp.ithome.com.tw/upload/images/20210906/20119044inGeS7PSQX.png

假如今天feature/b2也修改了同一個檔案,並且嘗試給develop merge呢?會產生衝突

git merge --no-ff feature/b2
# 噴錯
# Auto-merging Apple.txt
# CONFLICT (content): Merge conflict in Apple.txt
# Automatic merge failed; fix conflicts and then commit the result

此時進入人工調整merge的時候,此時visual studuio會自動偵測那些地方有衝突,你必須手動去修改這些衝突。點選incomming(要變的)還是accept current (保留原始code)

https://ithelp.ithome.com.tw/upload/images/20210906/20119044SJLqYGp5cx.png\

決定後保存並commit,並且切換到b2,換他merge develop來同步最新的更新

git commit -am "合併b2"
git checkout feature/b2
git merge --no-ff develop

# 將這次的改變推上遠端倉庫
git checkout develop
git push origin develop

https://ithelp.ithome.com.tw/upload/images/20210906/20119044u6ZyNZG0Mc.png

可以看到紅色為develop主幹道,feature/b1(綠色)與feature/b2(藍色) 對develop進行merge,merge完後再繼續進行開發。 (如果沒有--no-ff分支會消失)


pull requests

簡單講一下正常的CI/CD軟體開發流程,通常開發人員不允許直接merge到master:

  1. 多人開設feature 對develop進行開發
  2. 當develop開發到一定版本後會進行pull requests到master(要求將develop的內容merge給master)。
  3. 通常master只有專案主管有權限修改,避免太多修改導致混亂。
  4. 專案技術主管省查develop的code後允許merge,(master) git merge develop
  5. master branch受到變動,因此啟用CI/CD流程,將會自動將master的程式部屬到production機器上

下面來講一下怎麼pull request,其實github點一點就好。

https://ithelp.ithome.com.tw/upload/images/20210907/20119044ioNF1Xa28T.png

此時Project Owner就會收到pull request的請求。點選merge pull requests即可。

https://ithelp.ithome.com.tw/upload/images/20210907/20119044UTbyGyQS5g.png


上一篇
Day 4 : Git 分支與遠端倉庫
下一篇
Day 6 : Github issue與project
系列文
DevOps的下克上之旅( ° ∀ ° )ノ゙30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言