Git: can't undo local changes (error: path … i

2019-01-16 00:08发布

I have following working tree state

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

File foo/bar.txt is there and I want to get it to the "unchanged state" again (similar to '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

Now it is getting confusing:

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

The same file in both sections, new and modified? What should I do?

5条回答
啃猪蹄的小仙女
2楼-- · 2019-01-16 00:46

This worked perfectly for me:

$ git reset -- foo/bar.txt
$ git checkout foo/bar.txt
查看更多
放我归山
3楼-- · 2019-01-16 00:46
git checkout foo/bar.txt

did you tried that? (without a HEAD keyword)

I usually revert my changes this way.

查看更多
ら.Afraid
4楼-- · 2019-01-16 01:02

You did it the wrong way around. You are meant to reset first, to unstage the file, then checkout, to revert local changes.

Try this:

$ git reset foo/bar.txt
$ git checkout foo/bar.txt
查看更多
ゆ 、 Hurt°
5楼-- · 2019-01-16 01:06
git checkout origin/[branch] .
git status

// Note dot (.) at the end. And all will be good

查看更多
神经病院院长
6楼-- · 2019-01-16 01:07

I find git stash very useful for temporal handling of all 'dirty' states.

查看更多
登录 后发表回答