在 Git 合併中使用 theirs 選項
在使用 Git 工具開發軟體時,你可以為不同的功能建立不同的分支。但是不同分支之間可能存在衝突。
本文將解釋如何使用帶有 theirs 選項的 git merge 命令來解決衝突。
在 Git 中將 git merge 命令與 theirs 一起使用
git merge 命令可以合併兩個或多個開發歷史。但是,由於分支之間的衝突,有時無法完成此合併。
ours 或 theirs 中的檔案應丟棄以解決此衝突。
ours 是指具有 Git 歷史許可權的原始工作分支,theirs 是指包含新應用提交的分支。
你可以使用 git merge -s ours 丟棄 ours 中的更改。此命令會丟棄來自其他分支的所有更改,並使你分支上的檔案保持不變。
當你下次從另一個分支合併時,Git 只會考慮從這一點開始所做的更改。但是,-s 選項不能與 theirs 一起使用。
使用 --strategy-option 解決 Git 中的衝突
或者,我們可以將 theirs 與 -X 或 --strategy-option 選項一起使用。
-X 和 -s 選項之間的主要區別在於 -X 執行常規遞迴合併,使用所選一側解決任何衝突,而 -s 將合併更改為完全忽略另一側。
以下命令使用 theirs 解決任何衝突。
git merge -X theirs branchname 在 Git 中使用臨時分支進行合併
使用下面的命令將 test2 合併到簽出的 test1 中。
切換到 test1 分支。
git checkout test1 合併提交而不會發生衝突。ours 的內容稍後將被丟棄。
git merge -s ours test2 建立一個臨時分支。
git branch temp 使用 git reset 命令設定 HEAD。它從 test2 分支獲取內容。
git reset --hard test2 重置為合併提交,但使用 git reset --soft 命令保留內容。
git reset --soft temp 用 test2 的內容更改合併提交的內容。
git commit --amend 刪除我們之前建立的臨時分支。
git branch -D temp 你可以看到合併後的提交僅包含 test2 的內容。
git diff HEAD test2 Yahya Irmak has experience in full stack technologies such as Java, Spring Boot, JavaScript, CSS, HTML.
LinkedIn