有没有办法冲突合并同一分支的过去提交?

Is there a way to conflict-merge past commits of a same branch?

所以这是交易:

我正在处理一个家庭作业文件。我 checkout 将 repo 编辑到学校计算机中,并向该 repo 添加了评论。但是在合并评论之前,我对部分源代码进行了更改。当我合并两个分支时,我使用了git checkout --ours,而我本应该保存分支A的更改部分和分支B的注释部分。更糟糕的是,我在合并后还做了更多的更改。

基本上是这样的

Source code          Source code  
-----------          ----------- 
Part A         ->    Part A Change source         
-----------          -----------  
Part B               Part B         
-----------          -----------   

                     Source code  
                     ----------- 
               ->    Part A         
                     -----------  
                     Part B Comments         
                     -----------       

这应该合并到

Source code        
-----------        
Part A Change source  And then More changes      
-----------          
Part B Comments                
----------- 

但是变成了

Source code        
-----------        
Part A Change source  And then more changes    
-----------          
Part B                 
----------- 

现在,我尝试通过 git merge 6883dbbb(评论标签)制造合并冲突,但它显示

Already up to date. 

我看到很多回复建议查看分支机构。但是,我想要单个文件的部分反转。因为我在初始合并后添加(并想保留)更多更改,所以我无法签出到合并点。对这个问题有什么建议吗?谢谢。

| *   commit 4a51c9ac25e88f29b57d345ec294413e0969f1b6
| |\  Merge: 98b00f5 688a3db
| | | Author: 
| | | Date:   Tue May 7 00:51:21 2019 +0900
| | | 
| | |     complete merge
| | | 
| | * commit 688a3dbbbf7dab71703bb3107f0d4451cf85da88
| | | Author: 
| | | Date:   Sun May 5 03:02:39 2019 +0900
| | | 
| | |     HW4 report initial finish part 1 & comments
| | | 
| * | commit 98b00f5db8844251b13dc98ac5d6314d4fc4180f
| | | Author: 
| | | Date:   Tue May 7 00:39:35 2019 +0900
| | | 
| | |     confirm everything works
| | | 
| * | commit a89a77a64336dad49d124a2e8470b84764b9660d
| | | Author:  
| | | Date:   Sun May 5 17:08:26 2019 +0900
| | | 
| | |     move testcase
| | | 
| * | commit 40c4977a813088c910c54002cd9ca56eb880f722
| |/  Author: 
| |   Date:   Sun May 5 17:07:01 2019 +0900
| |   
| |       fix bubble
| | 

我会简单地手动重新创建带有注释的提交:

git format-patch 6883dbbb --stdout | patch -p1
git commit -a -m 'Re-applied comments commit'

您所拥有的 有效 与合并后恢复合并相同。也就是说,git 认为分支上的提交是 "accounted for",但更改已撤消。 (不同之处在于合并本身取消了更改,而不是后来的提交,但效果是一样的。)

要解决这种情况,您需要创建新的提交来模仿分支上的提交。最简单的方法是使用 git rebase -f

唯一的问题是,您需要确定您的分支从 master 分支出来的提交(或者 "upstream" 分支是什么;为了方便,我们会说 master的讨论)- "obvious" 方法(如 merge-base)将无法像您预期的那样工作,因为现在可以从 master 访问您的整个分支。但是,如果您知道这只是一次提交,那就足够简单了:

git checkout branch
git rebase -f HEAD^

现在您可以再次合并 branch