[英]Git: Merge old commit into current head version
有一個文件有兩個感興趣的提交,都在Master
分支上,都只修改單個文件foo
:先前的提交AA
,以及HEAD
的當前版本。 我想合並文件的兩個版本,保留兩者的位,進入Master
HEAD
。
我做了一件我覺得最簡單的事情:
git checkout -b merge-purgatory AA
git commit -m "pulled foo back from previous commit for merging into HEAD."
git checkout master
git merge merge-purgatory
這只是用AA
版本覆蓋當前HEAD
版本的foo
。 嘗試了更詳細的git checkout -m
,同樣的結果:一個愚蠢的覆蓋。
我如何強制git將AA
版本的foo
視為與當前HEAD
版本的沖突合並?
如果git的合並沒有做你想要的,你可以改為:
foo
中沒有未提交的更改,例如確保git status
是干凈的。 AA
的版本覆蓋foo
: git show AA:foo > foo
foo
進行更改: git add -p foo
git checkout -- foo
-foo丟棄foo
所有其他更改 git commit
或者,如果您更喜歡使用圖形差異工具(例如meld
),您可以這樣做:
git show AA:foo > old-foo
meld old-foo foo
以前的答案不會保留更改的故事,因此您使用先前的提交來生成新的提交。 以下程序將保留這個故事。
git checkout -b merge-purgatory AA
在這里你需要稍微修改你的文件,例如你可以添加一個空行。 然后
git commit "pulled foo back from previous commit for merging into HEAD."
git checkout master
git merge --no-commit merge-purgatory
通過這種方式,合並將失敗,然后您需要做的就是解決沖突。 這對我有用。
git merge --no-commit merge-purgatory
至少會給你機會審查:在提交之前更改合並。
另請參閱“ 如何使用git-merge合並選擇性文件? ”中提出的技術,基於挑選或結帳。
關於強制手動合並,您可以在.gitatributes
文件中聲明該特定文件的合並策略設置為取消設置 。
執行三向合並
合並
The attribute merge affects how three versions of a file is merged when a file-level merge is necessary during git merge, and other commands such as git revert and git cherry-pick.
Unset
將當前分支中的版本作為暫定合並結果,並聲明合並存在沖突。 這適用於沒有明確定義的合並語義的二進制文件。
我使用以下方法擺脫它:
git checkout -f b855a13754fabf5cef6ff93ab00558608a839377 -- .
這會強制將commit id更改為當前分支(master),然后我從這些應用的更改中創建了一個新分支( checkout -b
),並從后者創建了一個合並請求。
以上都不適合我:-(
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.