我有一個問題,在我看來,git(版本 2.31.1)的不直觀的合并行為。讓我用一個小的玩具倉庫來演示。
git init
touch file1
git add file1
git commit -m "Initial commit"
git branch feature
現在我有一個帶有空檔案和兩個分支的提交,main并且feature指向該提交。現在我將主分支中的檔案更改為
line1
line2
line3
line4
line5
并提交更改
git add file1
git commit -m "Change file1"
接下來,我去另一個分支
git checkout feature
更改file1為
line1
line2
line
line4
line5
并再次提交更改
git add file1
git commit -m "Change file1"
生成的提交樹如下所示:
* b1beb63 - Change file1 (HEAD -> feature)
| * 32ea83d - Change file1 (main)
|/
* 2952256 - Initial commit
現在我切換回main分支并合并feature到main.
git checkout main
git config merge.conflictstyle diff3
git merge feature
正如預期的那樣, 中存在沖突file1。但是,沖突看起來像這樣:
<<<<<<< HEAD
line1
line2
line3
line4
line5
||||||| 2952256
=======
line1
line2
line
line4
line5
>>>>>>> feature
As you can see, git acts like the entire file is one large conflict. I would have expected it to look like this:
line1
line2
<<<<<<< HEAD
line3
||||||| 2952256
=======
line
>>>>>>> feature
line4
line5
which would be infinitely more user-friendly.
I found some threads where people had similar problems because of different line endings. This is not the case here as I edited the file in both branches on the same platform with the same editor.
My current solution for this type of merge is to find changes manually by doing a diff of the files between the two branches, i.e. git diff main:file1 feature:file1. This works, but is rather annoying.
Is there any way to get the behavior I would expect in this merge? If not, is there any good reason why this is not possible?
uj5u.com熱心網友回復:
tl;dr您的預期行為是沖突的無效 diff3 表示。你不能兩者兼得。
最初的提交解釋了令人難以忍受的細節。我會試著總結一下。
讓我們看看你的情況。你從這個開始。
# ancestor
# main
line1
line2
line3
line4
line5
# feature
line1
line2
line
line4
line5
您要求在所有三個檔案之間進行差異,這就是您得到的。
<<<<<<< HEAD
line1
line2
line3
line4
line5
||||||| 2952256
=======
line1
line2
line
line4
line5
>>>>>>> feature
原始檔案是空白的。HEAD 將其更改為添加了五行。功能添加了五行。這是三向沖突的正確表示。
讓我們看看你希望看到什么......
line1
line2
<<<<<<< HEAD
line3
||||||| 2952256
=======
line
>>>>>>> feature
line4
line5
這表示原始檔案包含 4 行。HEAD 和功能在 line2 和 line4 之間添加了一條線。
換句話說,它說你合并了這些檔案。
# ancestor
line1
line2
line4
line5
# main
line1
line2
line3
line4
line5
# feature
line1
line2
line
line4
line5
那不是真的,祖先是空白的。
您想要的減少對于 2 向沖突標記很好,但會擊敗 3 向沖突標記的點。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/387343.html
上一篇:git中的簡單參考與輕量級標簽
