Can't reset a file to a specific commit using Git

坚强是说给别人听的谎言 提交于 2019-12-10 12:34:17

问题


I have a modified file which I want to rever to whatever is in the latest commit but it's "stuck" there always being marked as modified.

$ git status
# On branch master
# 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:   index.php
#
no changes added to commit (use "git add" and/or "git commit -a")

I then try:

$git checkout -- index.php

But the output of git status is still the same. I then try:

$git reset --hard master
HEAD is now at 02c9613 test commit message

And the output of git status is STILL the same.

Any ideas on how I can get rid of the supposed changes on that file?


回答1:


You might be running into a whitespace issue try git config --global apply.whitespace nowarn for that.

If that doesn't work, I'd say you've run into a bug. Save the local clone for future reference (I hope it's not too big) and create a bug report. Especially the facts that:

  • you haven't modified the file yourself
  • other files don't show this problem

are signs that it might just not be you that made the mistake here. Whether you can reproduce the problem on a clean repo would be interesting information too.




回答2:


You have to remove index.php from the "index." Then can you checkout a different version.

git rm --cached index.php

Should do the trick. See:

http://www.kernel.org/pub/software/scm/git/docs/git-rm.html




回答3:


Have you tried:

$ git checkout master -f -- index.php

or

$ git checkout master -f

?

I can't see why this would work if reset did not, but it's worth a try.




回答4:


try git ls-files -m | xargs -i git update-index --assume-unchanged "{}"




回答5:


FWIW, I was able to resolve the issue by removing the .git subdirectories within the directories that were showing modified. Once the .git subdirectories (as the subdirectories were .git projects in and of themselves) were gone, the parent folders no longer showed modified. If the files in question are in the same folder as an unrelated .git folder, that might also have an effect.




回答6:


I was stuck in the pretty same mess. I had some files I just couldn't get rid of in git status. After trying to reset or checkout the files in any kind of way, I decided to actually add the problematic files and commit them. Git seemed happy with it. I then went back to the previous commit and the problem was solved, the problematic files had indeed disappeared.

It doesn't explain the bug, but if this solution can solve it that's aleady a good thing.



来源:https://stackoverflow.com/questions/4441906/cant-reset-a-file-to-a-specific-commit-using-git

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!