git checkout -- <files> 不丢弃更改?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11458926/
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:06:38  来源:igfitidea点击:

git checkout -- <files> doesn't discard changes?

macosgitgit-checkout

提问by Highway of Life

I have changes in my working directory that I'm trying to discard (reset to the current indexed version of the files), however, git checkout -- <file>will not discard the changes. I've attempted to manually remove the files (rm -r files) then run git checkout -- ., which displays the files as modified again.

我在我的工作目录中有我试图放弃的更改(重置为文件的当前索引版本),但是,git checkout -- <file>不会放弃这些更改。我尝试手动删除文件 ( rm -r files) 然后运行git checkout -- .,这会再次将文件显示为已修改。

$ git checkout -- .
$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   files/Hulk.png
#   modified:   files/Hulk_2.png
#
no changes added to commit (use "git add" and/or "git commit -a")

Running git diffshows the files are modified...

运行git diff显示文件被修改...

diff --git a/files/Hulk.png b/files/Hulk.png
index 1c256cb..1d37fe0 100644
Binary files a/files/Hulk.png and b/files/Hulk.png differ
diff --git a/files/Hulk_2.png b/files/Hulk_2.png
index 1c256cb..0717199 100644
Binary files a/files/Hulk_2.png and b/files/Hulk_2.png differ

NOTE: Some people have said to run git checkout ., however this will achieve the same result as git checkout -- .. The --is just a notation used in the git checkout command to differentiate treeish/commit points from files/paths.

注意:有些人说要运行git checkout .,但是这将达到与git checkout -- .. 这--只是 git checkout 命令中用来区分树状/提交点与文件/路径的符号。

OS: OSX 10.6 Git: 1.7.10.2

操作系统:OSX 10.6 Git:1.7.10.2

回答by Highway of Life

The cause for this was due to multiple files with the same name but different cases. In OSX, which is case-insensitive, doesn't like multiple files with the same name but different cases. It views them as the same file. To fix this, I ran git mv(or just mv) to a temporary filename, added the temp files, which allowed git to remove the old/incorrectly named versions, then a second commit to name them back. This could also be corrected on a filesystem which allows different files with the same name to be different cases.

造成这种情况的原因是多个文件同名但大小写不同。在不区分大小写的 OSX 中,不喜欢多个名称相同但大小写不同的文件。它将它们视为同一个文件。为了解决这个问题,我运行git mv(或只是mv)到一个临时文件名,添加了临时文件,这允许 git 删除旧的/错误命名的版本,然后第二次提交以重新命名它们。这也可以在允许具有相同名称的不同文件为不同情况的文件系统上更正。

回答by vinboxx

Did you try

你试过了吗

git config --global core.autocrlf false

or

或者

git config --global core.filemode false

回答by Christopher

Based on your comments, you must configure your repository to be case sensitive:

根据您的评论,您必须将存储库配置为区分大小写:

git config core.ignorecase false

This allows git to track both files (although the file system only shows one, which is enormously confusing). Here are replication steps to demonstrate what's happening, when git is correctly tracking case sensitivity:

这允许 git 跟踪两个文件(尽管文件系统只显示一个,这非常令人困惑)。以下是复制步骤来演示当 git 正确跟踪区分大小写时发生了什么:

git init /tmp/test && cd /tmp/test
git config core.ignorecase false
echo test>test && git add test && git commit -m "lowercase t"
mv test Test

Now git statusshows no differences to test:

现在git status显示没有区别test

git status -s
 ?? Test

Commit Testand use git ls-filesto see what we're now tracking:

提交Test并使用git ls-files以查看我们现在正在跟踪的内容:

git add Test && git commit -m "uppercase T"
git ls-files
 Test
 test

What does lsreport? Why, just 'Test', naturally:

什么ls报告?为什么,只是“测试”,自然:

ls
 Test

Finally, what happens when we modify Test?

最后,当我们修改 Test 时会发生什么?

echo garbage>Test
git status -s
 M Test
 M test

What a mess.

真是一团糟。

回答by bluesman

Use . instead of --

用 。代替 -

git checkout .

回答by Igor

For some reason this same thing happened to me, but it was not a case-sensitivity problem. Deleting the file, then changing branches resolved the issue.

出于某种原因,同样的事情发生在我身上,但这不是区分大小写的问题。删除文件,然后更改分支解决了问题。

回答by mpasko256

What helped in my case: (also improving vinboxx's answer)

对我的情况有什么帮助:(也改进了 vinboxx 的答案)

I had got configured .gitattributesin a following way:

.gitattributes通过以下方式进行了配置:

* text eol=crlf

(I wanted -X renormalize to automatically use proper format after merge)

(我希望 -X renormalize 在合并后自动使用正确的格式)

After temporarily commenting that line along with setting git config core.autocrlf false. Everything went back to normal.

在临时评论该行以及设置之后git config core.autocrlf false。一切恢复正常。

回答by Jan Swiatecki

To avoid this problem next time, I decided to convert my file system to case-sensitive.

为了下次避免这个问题,我决定将我的文件系统转换为区分大小写。

I was pleasantly surprised that it is pretty easy on Mac OS (10.13.4).

我惊喜地发现它在 Mac OS (10.13.4) 上非常简单。

It's possible to create quickly a new disk-volume with a different file system:

可以使用不同的文件系统快速创建新的磁盘卷:

  • run Disk Utility
  • click on Container
  • click on Volume (+)
  • 运行磁盘工具
  • 点击容器
  • 单击音量 (+)

I named new volume "Projects" and set Format to APFS (Case-sensitive).

我将新卷命名为“项目”并将格式设置为 APFS(区分大小写)。

After that I moved all projects to the new volume.

之后,我将所有项目移至新卷。

回答by Badal

I faced the same problem. I found that the two problematic files had DOS line ending characters. I did this to fix the problem.

我遇到了同样的问题。我发现两个有问题的文件都有 DOS 行结束字符。我这样做是为了解决问题。

1- use a different clone to change the line ending to UNIX 2- blow away the clone where the problem was appearing and re clone it.

1- 使用不同的克隆将行结尾更改为 UNIX 2- 删除出现问题的克隆并重新克隆它。

回答by jugglinghobo

If you want to discard all changes you've made, just use

如果你想放弃你所做的所有更改,只需使用

git checkout .

回答by topdown

I used git checkout -- .

我使用了 git checkout -- 。

Also git checkout .

还有 git checkout 。

== the same thing as the op said both worked Mac OSX 10.7 and Linux, worked on both

== 与 op 所说的一样,都适用于 Mac OSX 10.7 和 Linux,适用于两者

with git version 1.7.7.5 (Apple Git-26) and git 1.7.1 compiled

使用 git 版本 1.7.7.5 (Apple Git-26) 和 git 1.7.1 编译

Try a different repo and see if you get the same results. Maybe a command bug in the git version?

尝试不同的 repo,看看是否得到相同的结果。也许是 git 版本中的命令错误?