git reset --hard 后留下未暂存的更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11383094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 14:05:36  来源:igfitidea点击:

Unstaged changes left after git reset --hard

gitgit-reset

提问by Norswap

After git reset --hard, git statusgives me files within the Changes not staged for commit:section.

之后git reset --hardgit status给我该Changes not staged for commit:部分中的文件。

I've also tried git reset ., git checkout -- .and git checkout-index -f -a, to no avail.

我也试过git reset .git checkout -- .git checkout-index -f -a,都无济于事。

So, how can I get rid of those unstaged changes?

那么,我怎样才能摆脱那些未分阶段的变化呢?

This seems to hit only Visual Studio project files. Weird. See this paste: http://pastebin.com/eFZwPn9Z. What is special with those files, is that in .gitattributes I have:

这似乎只影响 Visual Studio 项目文件。奇怪的。请参阅此贴:http: //pastebin.com/eFZwPn9Z。这些文件的特别之处在于,在 .gitattributes 中我有:

*.sln        eol=crlf
*.vcproj     eol=crlf
*.vcxproj*   eol=crlf

Also, autocrlfis set to false in my global .gitconfig. Could that be somehow relevant?

此外,autocrlf在我的 global 中设置为 false .gitconfig。这可能有某种意义吗?

采纳答案by Norswap

Okay, I've kind of solved the problem.

好的,我已经解决了这个问题。

It seemed that the .gitattributesfile, containing:

.gitattributes文件似乎包含:

*.sln        eol=crlf
*.vcproj     eol=crlf
*.vcxproj*   eol=crlf

made the project files appear unstaged. I am clueless why that is, and I'm really hoping that someone privy to the ways of git will give us a nice explanation.

使项目文件显示为未暂存。我不知道为什么会这样,我真的希望知道 git 方法的人能给我们一个很好的解释。

My fix was to remove these files, and to add autocrlf = falseunder [core]in .git/config.

我的修复是删除这些文件,并添加autocrlf = false[core].git/config

This does not amount to exactly the same thing as the previous configuration, as it requires every dev to have autocrlf = false. I'd like to find a better fix.

这与之前的配置并不完全相同,因为它要求每个开发人员都拥有autocrlf = false. 我想找到更好的解决方法。

EDIT:

编辑:

I commented the incriminating lines, uncommented them and it worked. What the ... I don't even ... !

我评论了有罪的台词,取消了它们的注释并且它起作用了。什么……我什至没有……!

回答by GameScripting

I had the same problem and it was related to the .gitattributesfile. However the file type that caused the problem was not specified in the .gitattributes.

我有同样的问题,它与.gitattributes文件有关。但是,导致问题的文件类型未在.gitattributes.

I was able to solve the issue by simply running

我能够通过简单地运行来解决这个问题

git rm .gitattributes
git add -A
git reset --hard

回答by Jacek Szybisz

RESOLVED!!!

解决!!!

I have resolved this problem using following steps

我已经使用以下步骤解决了这个问题

1) Remove every file from Git's index.

1) 从 Git 的索引中删除每个文件。

git rm --cached -r .

2) Rewrite the Git index to pick up all the new line endings.

2) 重写 Git 索引以获取所有新行结尾。

git reset --hard

Solution was part of steps described on git site https://help.github.com/articles/dealing-with-line-endings/

解决方案是 git 站点https://help.github.com/articles/dealing-with-line-endings/上描述的步骤的一部分

回答by YuriAlbuquerque

Git won't reset files that aren't on repository. So, you can:

Git 不会重置不在存储库中的文件。这样你就可以:

$ git add .
$ git reset --hard

This will stage all changes, which will cause Git to be aware of those files, and then reset them.

这将暂存所有更改,这将使 Git 知道这些文件,然后重置它们。

If this does not work, you can try to stash and drop your changes:

如果这不起作用,您可以尝试隐藏和删除您的更改:

$ git stash
$ git stash drop

回答by vezenkov

If you use Git for Windows, this is likely your issue

如果您使用 Git for Windows,这可能是您的问题

I've had the same problem and stash, hard reset, clean or even all of them was still leaving changes behind. What turned out to be the problem was the x file mode that was not set properly by git. This is a "known issue" with git for windows. The local changes show in gitk and git status as old mode 100755 new mode 100644, without any actual file differences.

我遇到了同样的问题,存储、硬重置、清理甚至所有这些都仍然留下更改。原来问题是 git 没有正确设置 x 文件模式。这是 git for windows 的“已知问题”。本地更改在 gitk 和 git status 中显示为旧模式 100755 新模式 100644,没有任何实际文件差异。

The fix is to ignore the file mode:

解决方法是忽略文件模式:

git config core.filemode false

More info here

更多信息在这里

回答by Raman

Possible Cause #1 - Line Ending Normalization

可能的原因 #1 - 行结束规范化

One situation in which this can happen is when the file in question was checked into the repository without the correct configuration for line endings (1) resulting in a file in the repository with either the incorrect line endings, or mixed line endings. To confirm, verify that git diffshows only changes in line endings (these may not be visible by default, try git diff | cat -vto see carriage returns as literal ^Mcharacters).

可能发生这种情况的一种情况是,当有问题的文件在没有正确配置行尾 (1) 的情况下被签入存储库时,导致存储库中的文件具有不正确的行尾或混合行尾。要确认,请确认git diff仅显示行结尾的更改(默认情况下这些更改可能不可见,请尝试git diff | cat -v将回车视为文字^M字符)。

Subsequently, someone probably added a .gitattributesor modified the core.autocrlfsetting to normalize line endings (2). Based on the .gitattributesor global config, Git has applied local changes to your working copy that apply the line ending normalization requested. Unfortunately, for some reason git reset --harddoes not undo these line normalization changes.

随后,有人可能添加.gitattributes或修改了core.autocrlf设置以规范行尾 (2)。基于.gitattributes或全局配置,Git 已将本地更改应用到您的工作副本,这些更改应用了所请求的行结束规范化。不幸的是,由于某种原因git reset --hard不能撤消这些行规范化更改。

Solution

解决方案

Workarounds in which the local line endings are reset will not solve the problem. Every time the file is "seen" by git, it will try and reapply the normalization, resulting in the same issue.

重置本地行尾的变通方法不会解决问题。每次 git “看到”该文件时,它都会尝试重新应用规范化,从而导致相同的问题。

The best option is to let git apply the normalization it wants to by normalizing all the line endings in the repo to match the .gitattributes, and committing those changes -- see Trying to fix line-endings with git filter-branch, but having no luck.

最好的办法是让混帐通过回购全部归行尾匹配应用它想要的正常化.gitattributes,并承诺这些变化-看试图修复行结束用git过滤分支,但没有运气

If you really want to try and revert the changes to the file manually then the easiest solution seems to be to erase the modified files, and then to tell git to restore them, although I note this solution does not seem to work consistently 100% of the time (WARNING: DO NOTrun this if your modified files have changes other than line endings!!):

如果您真的想尝试手动还原对文件的更改,那么最简单的解决方案似乎是擦除修改后的文件,然后告诉 git 恢复它们,尽管我注意到此解决方案似乎无法 100% 始终如一地工作时间(警告:如果修改后的文件有除行尾以外的变化,请不要运行它!!):

    git status --porcelain | grep "^ M" | cut -c4- | xargs rm
    git checkout -- .

Note that, unless you do normalize the line endings in the repository at some point, you will keep running into this issue.

请注意,除非您在某个时候对存储库中的行尾进行规范化,否则您将继续遇到此问题。

Possible Cause #2 - Case Insensitivity

可能的原因 #2 - 不区分大小写

The second possible cause is case insensitivity on Windows or Mac OS/X. For example, say a path like the following exists in the repository:

第二个可能的原因是在 Windows 或 Mac OS/X 上不区分大小写。例如,假设存储库中存在如下所示的路径:

/foo/bar

/foo/bar

Now someone on Linux commits files into /foo/Bar(probably due to a build tool or something that created that directory) and pushes. On Linux, this is actually now two separate directories:

现在 Linux 上的某个人将文件提交到/foo/Bar(可能是由于构建工具或创建该目录的东西)并推送。在 Linux 上,这实际上是两个独立的目录:

/foo/bar/fileA
/foo/Bar/fileA

Checking this repo out on Windows or Mac may result in modified fileAthat cannot be reset, because on each reset, git on Windows checks out /foo/bar/fileA, and then because Windows is case insensitive, overwrites the content of fileAwith /foo/Bar/fileA, resulting in them being "modified".

在 Windows 或 Mac 上检出这个 repo 可能会导致fileA无法重置的修改,因为每次重置时,Windows 上的 git 都会检出/foo/bar/fileA,然后因为 Windows 不区分大小写,覆盖fileAwith的内容/foo/Bar/fileA,导致它们被“修改”。

Another case may be an individual file(s) that exists in the repo, which when checked out on a case insensitive filesystem, would overlap. For example:

另一种情况可能是存储库中存在的单个文件,当在不区分大小写的文件系统上检出时,会重叠。例如:

/foo/bar/fileA
/foo/bar/filea

There may be other similar situations that could cause such problems.

可能还有其他类似的情况会导致此类问题。

git on case insensitive filesystems should really detect this situation and show a useful warning message, but it currently does not (this may change in the future -- see this discussionand related proposed patches on the git.git mailing list).

不区分大小写的文件系统上的 git 应该真正检测到这种情况并显示有用的警告消息,但目前还没有(这可能会在未来发生变化 - 请参阅此讨论和 git.git 邮件列表上的相关建议补丁)。

Solution

解决方案

The solution is to bring the case of files in the git index and the case on the Windows filesystem into alignment. This can either be done on Linux which will show the true state of things, OR on Windows with very useful open source utility Git-Unite. Git-Unite will apply the necessary case changes to the git index, which can then be committed to the repo.

解决方案是将 git 索引中的文件大小写与 Windows 文件系统上的大小写对齐。这可以在 Linux 上完成,这将显示事物的真实状态,或者在 Windows 上使用非常有用的开源实用程序Git-Unite 完成。Git-Unite 将对 git 索引应用必要的案例更改,然后可以将其提交到存储库。

(1) This was most likely by someone using Windows, without any .gitattributesdefinition for the file in question, and using the default global setting for core.autocrlfwhich is false(see (2)).

(1)这是最有可能使用Windows的人,没有任何.gitattributes有问题的文件定义,并使用默认的全局设置的core.autocrlffalse(见(2))。

(2) http://adaptivepatchwork.com/2012/03/01/mind-the-end-of-your-line/

(2) http://adaptivepatchwork.com/2012/03/01/mind-the-end-of-your-line/

回答by joshuakcockrell

If other answers are not working, trying adding the files, then resetting

如果其他答案不起作用,请尝试添加文件,然后重置

$ git add -A
$ git reset --hard

In my case, this helped when there were a bunch of empty files that git was tracking.

就我而言,当 git 正在跟踪一堆空文件时,这会有所帮助。

回答by Ohad Schneider

Another cause for this might be case-insensitive file systems. If you have multiple folders in your repo on the same level whose names only differ by case, you will get hit by this. Browse the source repository using its web interface (e.g. GitHub or VSTS) to make sure.

另一个原因可能是不区分大小写的文件系统。如果您的存储库中有多个位于同一级别的文件夹,其名称仅因大小写而异,您将受到此影响。使用其 Web 界面(例如 GitHub 或 VSTS)浏览源存储库以确保。

For more information: https://stackoverflow.com/a/2016426/67824

更多信息:https: //stackoverflow.com/a/2016426/67824

回答by Shahbaz

You can stashaway your changes, then drop the stash:

您可以stash删除您的更改,然后删除存储:

git stash
git stash drop

回答by mrks

Run cleancommand:

运行清理命令:

# Remove all untracked files and directories. (`-f` is `force`, `-d` is `remove directories`)

git clean -fd