兩個人各自新增一個功能,剛好都改到同一行設定。Git 會停下來,等我們決定最後要保留什麼。這篇在本機建立兩個分支,實際做出一次衝突,再用 git status 找到檔案、比對三個版本,最後確認兩邊的改動都進了合併提交。
範例在 Windows PowerShell 執行,使用 Git 2.28 以上。請從平常用來放練習檔的資料夾開始,下面的指令會建立一個新資料夾。剛開始學分支和暫存區,可以先看《Git使用教學及GitLab的基本操作》。
1. 建立練習用的儲存庫
git --version
$lab = Join-Path $PWD ('git-merge-lab-' + [guid]::NewGuid().ToString('N').Substring(0, 8))
New-Item -ItemType Directory -Path $lab -ErrorAction Stop | Out-Null
Set-Location -LiteralPath $lab -ErrorAction Stop
git init -b main
git config --local user.name 'Git Merge Lab'
git config --local user.email 'git-merge-lab@example.invalid'
Set-Content -LiteralPath features.conf -Value "features=ping`n" -Encoding Ascii -NoNewline
git add -- features.conf
git commit -m 'Add default feature flags'features.conf 只有一行,逗號分隔各項功能。範例一律寫入 LF 換行,讓各版本的檔尾一致。第一筆提交完成後,在 main 上執行 git status --short,應該沒有任何輸出。git config --local 設定的身分只作用在這個練習儲存庫。
2. 讓兩個分支各自修改同一行
git switch -c feature/upload
Set-Content -LiteralPath features.conf -Value "features=ping,upload`n" -Encoding Ascii -NoNewline
git add -- features.conf
git commit -m 'Enable upload'
git switch main
Set-Content -LiteralPath features.conf -Value "features=ping,search`n" -Encoding Ascii -NoNewline
git add -- features.conf
git commit -m 'Enable search'
git status --short兩個分支從相同的 features=ping 出發。feature/upload 加了 upload,main 加了 search。兩邊都已有提交,工作區也乾淨,現在從 main 發起合併。
git -c merge.conflictStyle=merge merge --no-ff feature/upload
git status --short第一行會回報 CONFLICT (content),這是這個練習預期的結果。git status --short 會列出 UU features.conf。UU 代表兩邊都修改了同一個檔案,目前仍未解決。Git 官方文件的狀態代碼
3. 比對共同基準和兩邊的修改
執行 Get-Content features.conf,會看到 Git 留在工作區的衝突標記。HEAD 是目前的 main,分隔線下方是正要合併進來的 feature/upload。
<<<<<<< HEAD
features=ping,search
=======
features=ping,upload
>>>>>>> feature/upload如果光看標記還無法判斷哪一邊新增了什麼,可以在 git add 之前逐一讀取 Git 保留在暫存區的三個版本。
git --no-pager show ':1:features.conf'
git --no-pager show ':2:features.conf'
git --no-pager show ':3:features.conf'這三行依序顯示共同基準 features=ping、目前分支 features=ping,search,以及要合併進來的版本 features=ping,upload。這樣就能看出兩邊各增加了一項功能,這一題應該把它們合併成同一行。Git 官方文件的合併衝突解法
4. 存下解法,再確認暫存內容
用編輯器把檔案內容改成一行 features=ping,search,upload,同時移除所有衝突標記。練習檔只有這一行,也可以直接用下面的指令寫入。
Set-Content -LiteralPath features.conf -Value "features=ping,search,upload`n" -Encoding Ascii -NoNewline
git add -- features.conf
git status --short
git diff --cached --check
git --no-pager diff --cached -- features.confgit status --short 現在會顯示 M features.conf,表示解法已暫存。git diff --cached --check 正常時沒有輸出。若有提示,先修正並重新執行 git add。最後一行的 diff 應顯示將 features=ping,search 改成 features=ping,search,upload。正式專案也要執行測試,確認解法符合程式預期。Git 官方的 diff 參考文件
5. 完成合併並確認有兩個父提交
git commit -m 'Merge feature/upload'
git status --short
Get-Content features.conf
git --no-pager log --graph --oneline --all
git --no-pager show -s --format='%s%n%P' HEAD完成後,工作區應該是乾淨的,檔案仍是 features=ping,search,upload。最後一行指令會先印出提交訊息,再印出由空白分隔的兩個雜湊。兩個雜湊對應合併前的 main 與 feature/upload,代表這筆提交把兩段歷史接在一起。
練習卡住時怎麼處理
- 想先取消這次合併。在合併提交之前執行
git merge --abort,練習用的features.conf會回到features=ping,search。合併期間編寫的解法也會被撤回,要保留請先另外存一份。實務上應先處理合併前還沒提交的改動。 - 改完檔案卻還是顯示
UU。先確認檔案已儲存,再執行git add -- features.conf。儲存檔案只更新工作區,Git 要等加入暫存區才會把它視為已解決。 - 合併沒有出現衝突。用
git branch --show-current確認目前在main,再用git --no-pager log --graph --oneline --all確認兩個分支各有一筆修改同一行的提交。已完成合併時,可以從第 1 步建立另一個練習資料夾。 - 提交時出現簽章錯誤。如果電腦要求簽署每一筆提交,可以在這個練習儲存庫執行
git config --local commit.gpgsign false,再重試當前的git commit。
開源專案與作者
Git 由 Linus Torvalds 發起,後續由 Git 開源社群共同開發與維護。Git 原始碼 · Git 專案沿革
發佈留言