Git合并冲突:找不到问题

问题描述

一些背景故事!
我一直在使用 git 为一个开源包做出贡献,我在这方面有点新手。我知道一些基本的东西。此外,我还看到如果发生合并冲突,我们可以遍历文件标记可以帮助我们找到同一文件的两个版本中相互冲突的代码块。

最近,我第一次遇到了合并冲突,它是在 Jupyter notebook 文件 (.ipynb) 上。但是,合并冲突的标记使 Jupyter 无法读取笔记本。所以,我尝试了 JSON 编辑器和 VS CODE 和 Notepad++。虽然我现在可以看到该文件,但它真的很乱,因为它不仅包含 Jupyter notebook 的所有元数据,而且甚至在执行次数方面也显示冲突。另外,如果笔记本中有一个图形作为一个单元格的输出,那么它被转换成很多字符,向下滚动浏览它们很头疼。

在google和stackoverflow上搜索并与项目所有者讨论这个问题后,我们决定采取另一种方法。但是,它不起作用。 (我在下面解释过,但首先我需要提供更多信息。请耐心等待)

根据提交的历史记录,我应该是在本地通过更改父分支上的相同文件而在本地创建此类冲突的人,因为没有人在上游(在原始存储库中)更改它

因此,请让我首先简要介绍一下分支和我对文件所做的一些更改。

  • 分叉和克隆后,我创建了一个分支 branch-A
  • 我更改了笔记本“notebook-file”
  • 我做了拉取请求 (PR)
  • 我创建了 A 的一个子分支,我们称之为 sub-branch-of-A
  • 切换到子分支后,我更改了两个 .py 文件,然后也更改了 notebook-file
  • sub-branch-of-A 合并到 branch-A

在我的 branch-A 中,我做了:git push origin branch-A 但我收到了 non-fast-forward 错误。这意味着发生了分歧。对?所以,我做了 git pull origin branch-A解决它,但我遇到了 notebook-file 的合并冲突。

替代解决方

所以,我被告知我可以将文件复制到本地 git 存储库之外的某个地方,然后执行 git checkout notebook-file 以在发生分歧的父节点中获取文件。对?然后,如果我执行 git pull ... 应该没有问题(然后我可以包含该复制文件的更改)

但是...

我再次遇到合并冲突错误。我疯了,尝试了几件事,但仍然没有。

我附上了下面的 git 日志。 我说的分支是Snippets_Tutorial,它的子分支是Snippets_Regime。如果我没记错的话,我用 git checkout -b Snippets_Regime Snippets_Tutorial 创建了那个子分支。正如我之前提到的,我切换到子分支 Snippets_Regime 并对 somefile.py 文件notebook-file 进行了一些更改。然后,我将其合并到 Snippets_Tutorial 中。

$ git log --all --graph --decorate --oneline
* 8d62ec9 (merlin_Tutorial) modify the code for comparing merlin3 (MATLAB) and merlin (Python) implementation
* 4952e92 (origin/merlin_Tutorial) Use merlin (of Table3) on Mars Science Labratory data to reproduce fig. 17 of paper (top and botthm)
* 8d04aaa plot discords of NYC TAXI data that discovered by ONLY python or ONLT matlab
* 2bbb569 Revise DRAG,Implement merlin,and Try merlin on data
* 361824f correct the implementation of the algorithm to return correct NN index of the discords
* dd6eb87 Revise the implementation of DRAG algorithm provided in Table 1 and 2.
*   1adb0f6 Allow merge from main to my local branch
|\
* | 62e6b29 Add Tutorial for Matrix Profile XXI: merlin algorithm #Issue 417
| | * 81798ab (HEAD -> Snippets_Tutorial) copy back Snippet Tutorial notebook
| | * e680e0f allow git to track the file (?)
| | * eef7018 copy back Snippet Tutorial notebook to the Snipperts_Tutorial branch
| | * 15ebf32 remove snippet tutorial local file from its developing branch tto allow merge from the main
| | *   06c05e3 merge updated files from origin to my branch (?)
| | |\
| | |/
| |/|
| * | 4c05d54 (upstream/main,origin/main,origin/HEAD,main) Fixed #414 Added fork syncing,checkBox fix to Contribute.ipynb
|/ /
| * be9169c resolve issues raised by Flake8
| * c7a2654 (Snippets_Regime) update the notebook according to the updated version of snippet module
| * e4c0696 Change snippet_regime from list to numpy array
| * c6ab996 correct the block of code for plotting the snippets regimes
| * ed1b1a6 add the plot of snippets regime to check out the functionality of snippets_regime added to snippets modules
| * 52da7b2 remove blank line after docstring of a function
| * 6d599df use stumpy.mplstyle to refactor the rcparams
| * e968427 add the slices of indices for each snippet in the output
| * 910746e [WIP]: save temp changes
| * 911abaf [WIP] Revise the intro section and legend of figures
| * b47e727 Add textual context and Improve the flow and figures
| * b9ad95e Add the introduction part
|/
* 589630e Saved widget state in notebook
* 21abb19 Added empty array _get_array_ranges unit test
* 9f6c2cf Fixed #413 Edge Case in core._get_array_ranges
* db1c694 Updated STUMPY_EXCL_ZONE_DENOM test
* 7383cb7 Added gpu_stimp
* 1f1f426 Added stimped feature
* e87f9ad Fixed #411 Bad import of config settings
* 50a9089 Replaced mpl params with style file
* f80e6ce Fixed typo
* 000a0e6 Replaced nan_to_num in unit test
* c67fd56 Fixed #409 Added IPython to docs/requirements.txt; #409 (#410)
* 248ce34 Fixed typo in docstrings of subspace & _subspace (#406)
* c178889 Replaced nan_to_num with np.isnan
* 05cfc52 Fixed #237 Added STIMP (aka SKIMP) feature
* a59d57f Added bonus section on interpreting mp columns
* 9bf1944 Updated conda installation and environment
* 856dc75 Moved additional excl_zone parameter to config.py
* 48864d2 Added motif_idx with include/discords subspace example
* ebb3559 Added include/discords tutorial example to subspace
* 03bbdca Fixed grammar in contributor guide
* 3832b21 Fixed #385Clarifying the "Make your Changes" Section of Contributor Guide (#386)
| * 5f004a2 (origin/Snippets_Tutorial) [WIP] Revise the intro section and legend of figures
| * 0afad01 Add textual context and Improve the flow and figures
| * 9d0095a Add the introduction part
|/
| * 7a36675 (origin/Geometric_Chain,Geometric_Chain) Add chain analysis for respiration data and reproduce Table 2 of the paper
| * 01dc5d3 Correct/Update the markdowns and the result (adding discussion and figures)
| * e27b6b9 Correst docstrings of several functions
:

我附上同样的东西,但采用图像格式,因为我需要突出显示两个提交:

enter image description here

图像底部突出显示的提交是我在 fork 中看到的提交。 另一个突出显示的提交是我认为我可以通过在 git merge main <branch> 处执行 <branch>解决它。蓝色曲线包围的部分是合并的地方,不知道这里发生了什么(我的意思是,我无法完全记住我发疯时使用的确切 git 命令)

如果我遗漏了什么,有人可以帮助我/指导我吗?

解决方法

在合并冲突解决中重要的是两个技巧和基础,以及冲突。其他一切都是噪音。

git diff :1:that.file :2:that.file
git diff :1:that.file :3:that.file 

将向您展示 git 合并的两组更改。在几乎所有与 git diff MERGE_HEAD... that.filegit diff ...MERGE_HEAD that.file 相同的情况下,只有当 Git 解决由以前的交叉合并引入的异常差异时,即具有不同内容的多个等效合并基础,这在大多数历史中都不是你看到的,基础内容不是来自单个现有的基础提交。

,

在分布式版本控制系统中合并冲突是不可避免的,因为分支总是发散的。您想要的不是避免合并冲突,而是一种解决它们的有效方法。

问题——正如您亲身经历的那样——是 Jupyter Notebooks 的文件格式不是很容易手动编辑,因此 git 提供的所有默认工具来解决合并冲突对您没有多大帮助。

您需要的是 mergetool

有许多不同的合并工具以更加用户友好的方式呈现合并冲突,但它们无法解决您在 Jupyter Notebooks 格式方面的主要问题。

这就是 nbdime 来的地方。 nbdime 为您配置了 git,因此当发生冲突时,您可以使用其合并工具直观地解决冲突,而无需查看文本文件,因此可以避开所有 JSON 文件的噪音。

用Python的pip安装nbdime,然后配置git使用即可:

nbdime config-git --enable --global

您应该可以通过其合并工具来解释合并冲突。

nbdime mergetool