我有以下工作树状态

$ 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")

文件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

现在情况变得令人困惑了:

$ 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
#

两个部分都是同一个文件,新的和修改过的?我该怎么办?


git checkout foo/bar.txt

你试过了吗?(没有HEAD关键字)

我通常以这种方式恢复我的更改。


你做错了。您应该首先重置,取消文件,然后签出,恢复本地更改。

试试这个:

$ git reset foo/bar.txt
$ git checkout foo/bar.txt

我发现git stash非常有用的临时处理所有“脏”状态。


git checkout origin/[branch] .
git status

//在结尾加点。一切都会好起来的


这对我来说非常有效:

$ git reset -- foo/bar.txt
$ git checkout foo/bar.txt

在最近的git版本中,git恢复应该是一种比重载签出更好的恢复不需要的本地更改的方法。很好,这听起来很合理——一个用于普通操作的很好的简单专用工具。

但是,这是我最喜欢的小虫子。是的,我知道有些笨蛋会说:“这不是bug,这是设计出来的。”但是对于这样的用户界面,我坚持“bug”这个绰号:

% git restore LEGAL
error: path 'LEGAL' is unmerged
# okay, fine...
% git restore --ignore-unmerged LEGAL
warning: path 'LEGAL' is unmerged
# Arg, what?!

(由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.

天啊!我想解决用户界面问题的方法是将选项从——忽略未合并改为——除非在我们不希望允许的情况下——先咨询文档,然后再源代码,然后在你无法解决的情况下——然后等待,因为他们中的一半人会争论为什么它是正确的,而另一半人则主张增加四个以上的选项作为解决方案。

然后去社区寻找解决方案。我谅你也不敢。

显然,我没有让我的裁判在一个状态,树状的斑点可以解决从工作文件到暂存区域的提交…犯错指数吗?


如果上面的答案不管用,试试:

git reset -–merge

修复未合并路径错误的“现代”方法,使用restore命令(自git v2.23, 2019年8月起):

# unstage the file
git restore --staged myfile

# restore the file (lose your working dir changes)
git restore myfile