我有以下工作树状态
$ 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 reset foo/bar.txt
$ git checkout foo/bar.txt
这对我来说非常有效:
$ git reset -- foo/bar.txt
$ git checkout foo/bar.txt
git checkout origin/[branch] .
git status
// 注意最后的点 (.)。一切都会好的
在最近的 git 版本中,与重载的 checkout
相比,git restore
应该是一种“更好”的方式来恢复不需要的本地更改。太好了,这听起来很合理 - 一个用于常见操作的简单易用的专用工具。
但是,这是我新的“最喜欢的”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?!
(由 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-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
。
然后去社区寻找解决方法。我赌你。
显然,我没有让我的 refs 处于可以通过从工作文件到暂存区域的提交来解决树状 blob 的状态......错误索引?
如果上述答案不起作用,请尝试:
git reset -–merge
git checkout foo/bar.txt
你试过吗? (没有 HEAD 关键字)
我通常以这种方式恢复我的更改。
checkout
时的典型错误:$ git co path/to/file
=result=> error: path 'path/to/file' is unmerged
=>所以,首先运行:$ git reset path/to/file
,然后 git checkout path/to/file
应该可以工作。
不定期副业成功案例分享
$ git reset -- foo/bar.txt
$ git checkout -- foo/bar.txt
(注意中间的额外“--”)