【问题标题】:Manually resolve git conflict by overwriting the conflicting file with the final content通过使用最终内容覆盖冲突文件来手动解决 git 冲突
【发布时间】:2020-01-03 18:18:03
【问题描述】:

我处于 git 中发生合并冲突的情况,到目前为止一切都很好。合并后,我的存储库中有一个文件 foo.txt

<<<<<<< HEAD
line 1 from-foo-branch
line 2 in bar-branch
||||||| empty tree
=======
line 1 from-foo-branch
line 2 in bar-branch
sdfgsdfgsdfg
>>>>>>> dev_futurama

如果我使用非 git 编辑器打开冲突文件 foo.txt,这就是我所拥有的。我想用这个外部的非 git 编辑器解析文件。解决方案的外观如何,如何通过终端进行合并?

【问题讨论】:

  • 当然,但是如果您在一次提交中解决冲突并在另一次提交中进行任何其他更改,您将拥有更清晰的历史记录。合并不应该真的有新的工作。
  • 简单地说,a merge is just a commit like any other。 Git 不关心文件中的内容。
  • 将问题中列出的部分替换为合并后要保留的行。它们可能是来自一个分支或来自另一个分支的线条,或者它们的组合,或者您想放在那里的任何东西。在确定冲突行的外观后,保存文件并将其添加到索引 (git add foo.txt)。将其添加到索引意味着您已经解决了冲突。修复所有冲突,然后运行git merge --continue 以恢复合并操作。
  • 阅读关于merge conflicts的Git文档
  • 旁注:没有“Git 编辑器”:Git 没有内置编辑器,它使用您告诉它使用的任何编辑器。所以所有的编辑器都是非 Git 编辑器。许多确实具有编辑 Git 提交消息的特殊模式(通过识别 Git 在调用它们时使用的文件名模式),但它们仍然只是通用编辑器。

标签: git


【解决方案1】:

当然你可以用新文件的内容用合并标记覆盖文件。之后只需执行git add &lt;file&gt;git commit

【讨论】:

  • 有什么方法可以在没有合并标记的情况下合并?我这里有“最终解决的”文件,但没有合并标记
  • 这个问题不是很清楚。您提交的版本中不应有冲突标记。
  • 对不起,我的错。我读到“用合并标记覆盖文件”
  • 您描述的命令在下一次提交中产生了一个无效文件。它不是合并提交,而是常规提交。问题是关于解决合并中的冲突。
  • @axiac,看起来问题已被编辑。最初的问题是文件是否可以被覆盖。
【解决方案2】:

在您希望的任何编辑器中编辑文件。

如何设置需要的git编辑器?

 # Set the default git editor
 git config --global core.editor <your editor>

一旦冲突解决,只需保存文件,然后将其添加到索引并提交


提示:

如果您想同时添加一些更改而不是所有更改,请使用git add -p

这些是您可以在add -p 中执行的选项:

y - stage this hunk
n - do not stage this hunk
q - quit, do not stage this hunk nor any of the remaining ones
a - stage this and all the remaining hunks in the file
d - do not stage this hunk nor any of the remaining hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help

一旦您使用s,它就会选择可以被视为独立更改的代码块。如果你想拆分它,甚至更多,你将不得不使用e 编辑大块,然后将它添加回舞台区域。

【讨论】:

    猜你喜欢
    • 2015-09-23
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    • 1970-01-01
    • 2015-10-15
    • 2014-01-12
    相关资源
    最近更新 更多