首页 > 解决方案 > 解决 rebase/merge 中的单个 git 文件冲突采取他们的/采取我们的?

问题描述

在使用 git 时,假设有一个本地提交main和一个新的远程提交origin/main,我输入:

$ git pull --rebase

然后 git 将回退我的本地提交,应用远程提交,然后尝试合并我的本地提交。

现在假设合并失败,并且在一个二进制文件中存在冲突foo

You are currently rebasing branch 'main'.
  (fix conflicts and then run "git rebase --continue")

Changes to be committed:
  a
  b
  c

Unmerged paths:
  (use "git restore --staged <file>..." to unstage)
  (use "git add <file>..." to mark resolution)
    both modified:   foo

我想:

  1. 取文件 foo 的远程版本,完全覆盖我对 foo 的本地更改

或者

  1. 取文件 foo 的本地版本,完全覆盖对 foo 的远程更改

我为 (1) 运行什么命令?

$ git take-remote-version-??? foo
$ git rebase --continue

我为 (2) 运行什么命令?

$ git take-local-version-??? foo
$ git rebase --continue

标签: git

解决方案


一般来说,您想要解决它的方式(例如,忘记来自一个分支的更改)并不是真正正确的(您应该通过它们来查看发生了什么)。话虽如此,您获取文件的方式是在正在合并/重新定位的 2 个分支之一中,如下所示:

git show :2:path-to-file > path-to-file # get this file as it is on _my_ branch
git show :3:path-to-file2 > path-to-file2 # get this file as it is on _the other_ branch/revision
git add path-to-file path-to-file2
git rebase --continue

推荐阅读