[教學] git-22 合併分支衝突 - 討論區

[教學] git-22 合併分支衝突

文章瀏覽次數 1197 文章回覆數 0

特種兵

特種兵圖像(預設)

2020-04-06 22:30:03

From:1.161.134.13

其實我們在 合併分支 已經介紹過發生衝突的處理方式,

不過這次不一樣,因為當時發生衝突的是文字檔,還可以使用純文字編輯器開啟做修改,

那如果是圖檔、執行檔等無法直接編輯的二進位檔該怎麼處理衝突呢?

# 先切到 new_test 分支
$ git checkout new_test                                                         
 Switched to branch 'new_test'                                                   

# 複製一個 word 檔過來 
$ cp /d/生活班232小時課表--0610.doc .                                           

# 看一下檔案列表
$ ls                                                                            
 abc.txt  hello.txt  log.txt  ooqq.txt  test.py  生活班232小時課表--0610.doc     

# 將工作目錄存到暫存區
$ git add .                                                                     

# 將工作目錄存到儲存庫                                                                                 
$ git commit -m '增加一個 word 檔'                                              
 [new_test 0ceca24] 增加一個 word 檔                                             
  1 file changed, 0 insertions(+), 0 deletions(-)                                
  create mode 100644 "\347\224\237\346\264\273\347\217\255232\345\260\217\346\231
 \202\350\252\262\350\241\250--0610.doc"                                         

# 切換到 master 主幹道
$ git checkout master                                                           
 Switched to branch 'master'                                                     

# 我開啟了 D 槽 根目錄的 word 檔刪了一些文字存檔後複製到 master     
$ cp /d/生活班232小時課表--0610.doc .                                           

# 看一下檔案列表
$ ls                                                                            
 abc.txt  hello.txt  log.txt  ooqq.txt  test.py  生活班232小時課表--0610.doc     

# 將工作目錄存到暫存區
$ git add .                                                                     

# 將工作目錄存到儲存庫  
$ git commit -m '我也加了相同的 word 檔檔名進來'                                
 [master 2a621b5] 我也加了相同的 word 檔檔名進來                                 
  1 file changed, 0 insertions(+), 0 deletions(-)                                
  create mode 100644 "\347\224\237\346\264\273\347\217\255232\345\260\217\346\231
 \202\350\252\262\350\241\250--0610.doc"                                         

# 接下來我們在 master 合併 new_test
$ git merge new_test                                                            
 warning: Cannot merge binary files: 生活班232小時課表--0610.doc (HEAD vs. new_te
 st)                                                                             
 CONFLICT (add/add): Merge conflict in 生活班232小時課表--0610.doc               
 Auto-merging 生活班232小時課表--0610.doc                                        
 Automatic merge failed; fix conflicts and then commit the result.               

# 查看狀態
$ git status                                                                    
 On branch master                                                                
 You have unmerged paths.                                                        
   (fix conflicts and run "git commit")                                          
   (use "git merge --abort" to abort the merge)                                  

 Unmerged paths:                                                                 
   (use "git add <file>..." to mark resolution)                                  
         both added:      "\347\224\237\346\264\273\347\217\255232\345\260\217\34
 6\231\202\350\252\262\350\241\250--0610.doc"                                    

 no changes added to commit (use "git add" and/or "git commit -a")               

兩邊擁有同一個檔案且無法直接使用純文字編輯器來處理,經過協商,會有兩種可能:

  1. 保留 master 的檔案 git checkout --ours 檔名
  2. 保留 new_test 的檔案 git checkout --theirs 檔名
# 我們假設保留 new_test 的版本
$ git checkout --theirs 生活班232小時課表--0610.doc                             
 Updated 1 path from the index                                                   

# 接下來你應該知道要做什麼才對
$ git add .                                                                     

$ git commit -m '保留 new_test 的版本'                                          
 [master 8436216] 保留 new_test 的版本                                           

開啟這個 word 檔應該會跟 new_test 版本的內容一樣。