Git合并冲突:找不到问题。

3

一些背景故事!
我一直在使用git为开源软件做贡献,但在这个领域我有些新手。我知道一些基础知识。此外,我还了解到,如果发生合并冲突,我们可以检查文件标记,找到两个版本的同一文件中存在冲突的代码块。

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

在搜索谷歌和stackoverflow以及与项目所有者讨论后,我们决定采取另一种方法。但是,它没有成功。(我之后会解释,但首先我需要提供更多信息。请耐心等待)

根据提交历史,我应该是在本地创建了这样的冲突,可能是因为在父分支上更改了相同的文件,因为没有人在上游(in the original repo)更改它

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

  • 分叉并克隆后,我创建了一个分支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 repo之外的某个位置,然后执行git checkout notebook-file来获取在发生分歧的父节点中的文件。对吧?然后,如果我执行git pull ...就不应该有问题(然后我可以包括那个复制文件的更改)

但是......

我又一次遇到了合并冲突错误。我变得疯狂,尝试了几种方法,但仍然无济于事。

我在下面附上了 git log。 我所说的分支是 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

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

请问有人能帮助我或指导我是否有任何遗漏的东西吗?

2个回答

5

在合并冲突解决中最重要的是两个分支的端点,基础和冲突内容。其他所有东西都是噪音。

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 解决由先前的交叉合并引入的不寻常差异时,即具有不同内容的多个等效合并基础,而这并不是大多数历史记录中所见的内容,此时基础内容就不是来自单个现有基础提交。


谢谢你的回答。你的回答给了我一些线索,最终我使用 git show :1:that.file > that.file.common 命令获取了该共同文件的副本,并将其合并到其中一个分支中,然后手动添加更改。git diff 命令很好用,但它显示了笔记本的许多元数据更改,这使得对我来说很难处理。 - Nima S
请告诉我如何确认您的赏金,因为它给了我解决问题的思路。 - Nima S
2
接受答案后,我相信赏金将在延迟后自动授予。如果您可以教一个过滤器从笔记本中剥离无用的元数据,那么您可以对过滤后的结果进行差异比较,并查看textconv的内容。当您在寻找重要更改时,这非常方便,因为它可以去除文本上机器生成的装饰噪音。 - jthill

3
分布式版本控制系统中合并冲突是不可避免的,因为分支必然会分歧。你想要的不是避免合并冲突,而是一种有效的解决方法。问题在于——正如你亲身经历的那样——Jupyter Notebooks的文件格式不太容易手动编辑,因此git提供的所有默认工具都无法帮助你解决合并冲突。你需要的是一个叫做mergetool的工具。有很多不同的mergetools可以以更加用户友好的方式呈现合并冲突,但它们无法解决你在Jupyter Notebooks格式上的主要问题。这就是nbdime的用处。nbdime会为你配置git,因此当发生冲突时,你可以使用其合并工具在视觉上解决冲突,而无需查看文本文件,从而避开了JSON文件的所有噪音。只需使用Python的pip安装nbdime,然后配置git以使用它即可。
nbdime config-git --enable --global

您应该不会遇到使用合并工具解决合并冲突的问题。

nbdime mergetool


1
谢谢你的回答。我尝试了nbdime,但是如果我没记错的话,无法弄清楚如何让笔记本在视觉上看到差异。在进入合并冲突后,是否有任何特定的命令应该使用nbdime在笔记本中查看差异? - Nima S
我不确定 git merge 是否会自动处理这个问题,如果没有,当你遇到冲突时,你应该运行 git mergetool。如果你已经使用 ndime config-git 正确地配置了 git,那么工具将弹出显示差异。完成后,请运行 git merge --continue - FelipeC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接