Git:无法撤消本地更改(错误:路径...已取消合并)

mkl*_*mnn 315 git git-checkout git-reset

我有以下工作树状态

$ git status foo/bar.txt
# On branch master
# Unmerged paths:
#   (use "git reset HEAD <file>..." to unstage)
#   (use "git add/rm <file>..." as appropriate to mark resolution)
#
#       deleted by us:      foo/bar.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
Run Code Online (Sandbox Code Playgroud)

文件foo/bar.txt在那里,我想再次进入"未更改状态"(类似于'svn revert'):

$ git checkout HEAD foo/bar.txt
error: path 'foo/bar.txt' is unmerged
$ git reset HEAD foo/bar.txt
Unstaged changes after reset:
M       foo/bar.txt
Run Code Online (Sandbox Code Playgroud)

现在它变得令人困惑:

$ git status foo/bar.txt
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   foo/bar.txt
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   foo/bar.txt
#
Run Code Online (Sandbox Code Playgroud)

两个部分中的相同文件,新增修改?我该怎么办?

Igo*_*aka 524

你做错了.您应首先重置,取消暂存文件,然后结帐,以恢复本地更改.

试试这个:

$ git reset foo/bar.txt
$ git checkout foo/bar.txt
Run Code Online (Sandbox Code Playgroud)

  • 对我来说它需要一个:<br/> $ git reset - foo/bar.txt <br/> $ git checkout - foo/bar.txt <br/>(注意中间的额外" - ") (18认同)
  • 好的语法是"git reset HEAD file1 file2 ..."然后"git checkout - file1 file2 ......" (4认同)
  • 一个解释(关于做什么)会很棒...... (3认同)
  • 当投票最高的答案基本上只是说“你做错了”时,总是很有趣:) (2认同)

小智 46

这对我很有用:

$ git reset -- foo/bar.txt
$ git checkout foo/bar.txt
Run Code Online (Sandbox Code Playgroud)


Joe*_*yde 13

git checkout origin/[branch] .
git status
Run Code Online (Sandbox Code Playgroud)

//注意末尾的点(.).一切都会好的


Bru*_*nha 9

如果上述答案不起作用,请尝试:

git reset --merge
Run Code Online (Sandbox Code Playgroud)


Jua*_*uan 7

在最近的 git 版本中,git restore与重载的 .git 版本相比,它应该是恢复不需要的本地更改的“更好”方法checkout。太棒了,这听起来很合理 - 一个很好的简单的专用工具,用于常见操作。

但是,这是我新的“最喜欢的” git bug。是的,我知道一些 git-head 会说,“这不是一个错误,这是设计使然的”。但对于这种用户界面,我支持“bug”这个绰号:

% git restore LEGAL
error: path 'LEGAL' is unmerged
# okay, fine...
% git restore --ignore-unmerged LEGAL
warning: path 'LEGAL' is unmerged
# Arg, what?!
Run Code Online (Sandbox Code Playgroud)

(由 git 2.25.1 提供)

首先是小问题:当工具由于特定条件而拒绝执行某些操作时,这不仅仅是一个警告。至少应该说没有进行操作。现在我必须去调查该操作是否确实执行了(提示:没有执行)。

当然,第二个问题是显而易见的。现在,让我们看一下手册页条目,看看为什么这个出色的工具不能执行我告诉它的操作:

   --ignore-unmerged
       When restoring files on the working tree from the index, do not
       abort the operation if there are unmerged entries and neither
       --ours, --theirs, --merge or --conflict is specified. Unmerged
       paths on the working tree are left alone.
Run Code Online (Sandbox Code Playgroud)

圣烟!我想这里针对用户界面问题的 git-ish 修复是将选项从--ignore-unmerged重命名为--ignore-unmerged-except-in-cases-where-we-do-not-want-to-allow-that--consult-documentation-then-source-code-then-team-of-gurus-when-you-cannot-figure-it-out---and-wait-while-half-of-them-argue-about-why-it-is-right-as-is-while-the-other-half-advocate-adding-four-more-options-as-the-fix.

然后去社区寻找解决方案。我赌你。

显然,我的参考文献没有处于可以通过从工作文件到暂存区域的提交来解决树形斑点的状态...错误索引?