[英]During git merge, in the case of merge conflict, how do I accept all changes from a certain branch?
[英]How do I accept git merge conflicts from “their” branch for only a certain directory?
這是我的情況: git merge master
導致 50 個文件出現合並沖突。 我希望其中 45 個文件只從master
接受並完成,我想手動解決其余 5 個文件中的沖突。所有 45 個文件都在目錄some/dir
。 其他5個分散在其他地方。 如果我只想接受所有 50 個沖突的 master 更改,我會運行這個:
git merge -X theirs master
# OR (same thing):
git merge --strategy-option theirs master
但我不想那樣,就像我說的。 我想要更像這樣的東西:
git merge -X theirs master some/dir
以便它自動為some/dir
所有沖突選擇“他們的”(主人的)一方,否則讓我手動修復沖突。 然而,這並不存在。
所以,我的解決方法是這樣的:
簡短的介紹:
開始合並,在許多沖突文件中手動修復我需要的幾個文件。 備份我剛剛修復的任何文件。 中止合並。 使用git merge -X theirs master
重做合並以自動接受master
對所有文件中的所有沖突的更改。 手動將我的幾個手動修復的文件復制回 repo,並提交它們。
這避免了必須手動修復 50 個文件,當只有 5 個真正需要我注意時,這會非常乏味和耗時。
完整、詳細的步驟:
開始正常合並:
git merge master
手動解決我想要在其中執行的 5 個文件中的沖突,並保存每個文件。 然后,將它們復制到 repo 之外的位置(或至少在 repo 忽略的文件夾中)。 我現在有我手動解析的那 5 個文件的副本。 這是實現此目的的一種方法:
mkdir -p temp # create `temp` dir inside the repo. # Note: if you're not aware, the ".git/info/exclude" file in your repo is # an **untracked** form of a ".gitignore" file for the repo. We will use # this file below. # Add a `/temp/` entry to the ".git/info/exclude" file to keep # the repo from tracking the /temp/ dir, but withOUT using the # shared ".gitignore" file since this is my own personal setting # not everyone else on the team necessarily wants. echo -e "\\n# don't track this temporary folder for my arbitrary use\\n/temp/" \\ >> .git/info/exclude # Now manually make copies of your 5 files you just resolved conflicts in: cp some/dir/file1.cpp temp cp some/dir/file2.cpp temp cp some/dir/file3.cpp temp cp some/dir/file4.cpp temp cp some/dir/file5.cpp temp
執行git merge --abort
中止合並,然后執行git merge -X theirs master
重做合並,除了這次自動接受所有 50 個文件的所有 master 更改(即:對於所有沖突)。
現在,手動將我手動解析的備份副本從上面復制回各自文件頂部的存儲庫:
cp temp/file1.cpp some/dir cp temp/file2.cpp some/dir cp temp/file3.cpp some/dir cp temp/file4.cpp some/dir cp temp/file5.cpp some/dir
最后,執行git add -A
和git commit --amend
將它們修改為合並提交,瞧! 我有 45 個文件的自動分辨率和 5 個文件的手動分辨率。
有沒有更好的辦法?
我認為這種方式效果很好並且非常有效,但我願意學習替代方案,特別是如果它們更快或更容易。
解決沖突時,您可以直接從其他分支檢出文件。
因此,在執行git merge
並解決您需要修復的文件的沖突之后。
執行git checkout <branch you're merging> -- some/dir
這將僅將文件從其他分支移動到這些更改。 我相信這也會讓他們做好承諾的准備。
如果ours
分支有更改,您可以只在--
之后列出文件,而不僅僅是檢查整個目錄。
在 6 個月前投票並標記正確這個答案之后,我今天意識到在進行了一次非常大且非常拙劣的merge
,需要這個答案是錯誤的。 假設您這樣做了,以使用master
的最新更改更新您的feature_branch
:
git checkout feature_branch
git merge master
# conflicts result...
……還有很多沖突。 您會看到其中 25 個在some/dir
,並且您希望保留來自feature_branch
所有沖突更改,因此您執行以下 3 個命令中的任何一個(在本例中都是相同的):
git checkout -- some/dir
# OR
git checkout HEAD -- some/dir
# OR
git checkout feature_branch -- some/dir
好吧,你剛剛搞砸了! master
還在some/dir
添加了一些更改和新文件,其中許多與您的feature_branch
更改零沖突,所有這些都是您想要的(這是將最新的master
合並到您的feature_branch
!)但現在它們是一切都消失了,因為您只是用feature_branch
some/dir
覆蓋了它們。 那根本不是我們想要的!
所以,這是正確的答案:
# RIGHT ANSWER
# Keep only conflicting changes for all conflicts within files inside
# some/dir, from the "--ours" (`feature_branch` in this case) side.
git checkout --ours -- some/dir
同樣,這與此不同:
# WRONG ANSWER
# Overwrite this entire directory
git checkout feature_branch -- some/dir
因此,這是完整上下文中的正確答案:
# merge latest master into feature_branch to get those upstream changes
# from other people into your feature_branch
git fetch origin master:master
git checkout feature_branch
git merge master
# conflicts result here...
# Keep all changes from `feature_branch` for conflicts in some/dir
git checkout --ours -- some/dir
# OR, keep all changes from `master` for conflicts in some/dir
git checkout --theirs -- some/dir
git add some/dir
git merge --continue
完畢!
有關詳細信息和清晰度,請參閱我的回答: 根據 Git,“我們”是誰,“他們”是誰? . 該答案的“警告警告”部分涵蓋了這一點,盡管該答案是一個更全面的示例,並且更好地說明了問題。
我認為@Schleis 的回答很好。 如果您的回購壓縮合並無論如何,另一種選擇是......在您開始合並之前,只需執行以下操作:
git checkout gabriel.staples/MyBranch
git checkout develop path/You/Want/To/Accept/Everything
git add path/You/Want/To/Accept/Everything
git commit -m "Partially merge develop at SHA XXXXX (squash-style)"
git merge develop
<resolve conflicts>
git add path/To/Your/Hand/Resolved/Files
git merge --continue
如果你真的想要,你也可以使用以下方法使上述更接近真正的合並(給它一個父級):
git commit-tree aboveMergeSHA^{tree} -p `git merge-base develop pseudoMergeSHA` -p develop -m "Merge develop at SHA XXXXX"
但到那時,我認為您不妨使用@Schleis 的回答。 我不喜歡在不需要的時候手動惹父母...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.