似乎无法放弃 Git 中的更改

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

Can't seem to discard changes in Git

gitrevert

提问by

After seeing the following from the command line:

从命令行看到以下内容后:

# On branch RB_3.0.10
# 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.htm

I am trying to discard my changes by typing the command:

我试图通过键入以下命令来放弃我的更改:

git checkout -- index.htm

but when I re-run git status, it looks exactly the same. The checkout doesn't seem to be working. Am I doing something wrong? I am using GIT 1.6.1.2 on windows/cygwin.

但是当我重新运行 git status 时,它看起来完全一样。结帐似乎不起作用。难道我做错了什么?我在 windows/cygwin 上使用 GIT 1.6.1.2。

# On branch RB_3.0.10
# 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.htm

回答by Frank Martin

This has been bothering me for a while, almost every repo I'd check out had changes that I couldn't discard. Long story short, I tried all of the above, nothing worked. This is what I did to get things back to normal (on a Mac):

这一直困扰着我一段时间,几乎我检查的每个 repo 都有我无法丢弃的更改。长话短说,我尝试了上述所有方法,但没有任何效果。这是我为使事情恢复正常所做的(在 Mac 上):

Completely remove the autocrlf & safecrlf settings from ~/.gitconfig
Completely remove the autocrlf & safecrlf settings from your repo's local config ./.git/config
git rm --cached -r .
git reset --hard

回答by Nianliang

Here is my experience, set following variables in .git/config:

这是我的经验,在 中设置以下变量.git/config

[core]
    autocrlf = false
    safecrlf = false
    eol = crlf

then run $ git checkout HEAD ., and it works. but $ git checkout -- .not, strange!

然后运行$ git checkout HEAD .,它的工作原理。但$ git checkout -- .不是,奇怪!

* git version 1.9.3

* git 版本 1.9.3

回答by 1800 INFORMATION

What changes does git diffshow on the file? On windows, I've seen issues with line-endings causing issues like this. In that case, look at what settings you have for git config core.autocrlfand git config core.safecrlf. There is some documentation for these settings here.

git diff文件上显示了哪些更改?在 Windows 上,我见过导致此类问题的行尾问题。在这种情况下,请查看您对git config core.autocrlf和 的设置git config core.safecrlf。这里有一些关于这些设置的文档

I would say, if you are using git svnfor integration with subversion, then do make sure autocrlfis turned off. From what I can tell it is just broken in this configuration and it makes most of the tools think files have been changed, when you have done a checkoutto revert any changes.

我会说,如果您正在使用git svn与 subversion 的集成,那么请确保autocrlf已关闭。据我所知,它只是在此配置中被破坏了,并且当您执行 acheckout以还原任何更改时,它会使大多数工具认为文件已更改。

If you are seeing a problem where you do git checkout, and then git statusshows the file is still modified, and git diffshows the file is modified on every line in the file, then this is the problem you are seeing.

如果您在执行时看到问题git checkout,然后git status显示该文件仍在修改,并且git diff在文件中的每一行都显示该文件已修改,那么这就是您所看到的问题。

core.autocrlf

If true, makes git convert CRLF at the end of lines in text files to LF when reading from the filesystem, and convert in reverse when writing to the filesystem. The variable can be set to input, in which case the conversion happens only while reading from the filesystem but files are written out with LF at the end of lines. Currently, which paths to consider "text" (i.e. be subjected to the autocrlf mechanism) is decided purely based on the contents.

core.safecrlf

If true, makes git check if converting CRLF as controlled by core.autocrlf is reversible. Git will verify if a command modifies a file in the work tree either directly or indirectly. For example, committing a file followed by checking out the same file should yield the original file in the work tree. If this is not the case for the current setting of core.autocrlf, git will reject the file. The variable can be set to "warn", in which case git will only warn about an irreversible conversion but continue the operation. ...

核心.autocrlf

如果为 true,则在从文件系统读取时使 git 将文本文件行尾的 CRLF 转换为 LF,并在写入文件系统时反向转换。该变量可以设置为输入,在这种情况下,转换仅在从文件系统读取时发生,但文件在行尾用 LF 写出。目前,考虑“文本”的路径(即受到 autocrlf 机制的约束)纯粹是根据内容来决定的。

core.safecrlf

如果为 true,则让 git 检查由 core.autocrlf 控制的转换 CRLF 是否可逆。Git 将验证命令是否直接或间接修改了工作树中的文件。例如,提交一个文件然后检出同一个文件应该会在工作树中产生原始文件。如果 core.autocrlf 的当前设置不是这种情况,git 将拒绝该文件。该变量可以设置为“warn”,在这种情况下,git 只会对不可逆转的转换发出警告,但会继续操作。...

回答by hasen

I think you need to pass -f

我认为你需要通过 -f

From the man page (man git-checkout, GIT-CHECKOUT(1)):

从手册页(man git-checkout,GIT-CHECKOUT(1)):

-f, --force
Proceed even if the index or the working tree differs from HEAD.
This is used to throw away local changes.

-f, --force
即使索引或工作树与 HEAD 不同,也继续。
这用于丢弃本地更改

For instance, discard changes on the current branch and switch to a different branch:

例如,放弃当前分支上的更改并切换到不同的分支:

git checkout -f master

回答by Eyal

It might be line endings, as @1800-information suggests, but another possibility is that the difference (that's preventing your from reverting these files with a checkout command) is one of file mode. This is what happened to me. On my version of git you can discover this by using

正如@1800-information 所暗示的那样,它可能是行尾,但另一种可能性是差异(阻止您使用 checkout 命令恢复这些文件)是一种文件模式。这就是发生在我身上的事情。在我的 git 版本上,您可以通过使用来发现这一点

git diff index.htm

git diff index.htm

And it will show you file mode changes. It still won't let you revert them, though, using checkout, even with the -f option. For that use either

它将向您显示文件模式更改。但是,即使使用 -f 选项,它仍然不会让您使用 checkout 还原它们。为此使用

git config core.filemode false

git 配置 core.filemode 假

or change your git .config in your text editor by adding

或通过添加更改您的文本编辑器中的 git .config

[core]

filemode = false

[核]

filemode = false

After you do this, you can use

完成此操作后,您可以使用

git reset HEAD index.htm

git reset HEAD index.htm

and the file should disappear.

并且文件应该消失。

(I got all of this from the answers to How do I make git ignore mode changes (chmod)?and updating-file-permissions-only-in-git)

(我从How do I make git ignore mode changes (chmod)?updates-file-permissions-only-in-git的答案中得到了所有这些)

回答by Fil

Are you on OSX or Windows? If so, the problem probably is having two files of the same name, with different case. eg. index.htm and Index.htm

您使用的是 OSX 还是 Windows?如果是这样,问题可能是有两个同名文件,但大小写不同。例如。index.htm 和 Index.htm

Windows, and by default OSX, uses a case insensitive file system, which conflicts with the case sensitive git.

Windows 和默认的 OSX 使用不区分大小写的文件系统,这与区分大小写的 git 冲突。

回答by Eldar

I had this issue and after trying all of the above, nothing worked.

我遇到了这个问题,在尝试了上述所有方法后,没有任何效果。

What worked for me was to delete the directory that the file was in, then did git statusand made sure that all the files in that dir are now marked as deleted. After that I simply did git checkout -fand everything was back to normal.

对我有用的是删除文件所在的目录,然后删除git status并确保该目录中的所有文件现在都标记为已删除。在那之后,我只是做了git checkout -f,一切都恢复了正常。

回答by Waqleh

I was working on a libGDXproject on Android Studioand I wanted to discard all the changes that I have done, and nothing was working for me, the solution I came up with was to commit all the changes into a new branch

我正在做一个libGDX项目Android Studio,我想放弃我所做的所有更改,但没有任何效果对我有用,我想出的解决方案是将所有更改提交到一个新分支

git checkout -b TRASH
git add .
git commit -m "discarded changes"
git checkout master

and then you can delete the TRASHbranch if you want.

然后你可以根据需要删除TRASH分支。

回答by joe

I had the same problem, nothing from the above comments worked. It turned out, that my filesystem is not case sensitive (osx default, but windows probably behaves the same) and a file was present with both uppercase and lowercase in the same directory, with different content. Since on my computer both names pointed at the same file, git status always showed a modification, no matter what I did. To resolve the problem:

我遇到了同样的问题,上述评论没有任何效果。事实证明,我的文件系统不区分大小写(osx 默认,但 windows 可能表现相同)并且文件在同一目录中同时存在大写和小写,但内容不同。由于在我的计算机上两个名称都指向同一个文件,因此无论我做什么, git status 总是显示有修改。要解决问题:

  • I had to remove one of the files from another computer and push it to repo

  • delete the whole local version completely

  • do git clone from scratch

  • 我不得不从另一台计算机中删除其中一个文件并将其推送到 repo

  • 完全删除整个本地版本

  • 从头开始做 git clone

回答by Noumenon

I had a permissions problem in Windows and had to do icacls containingFolder /reset /t /l /cand then double-click the folder to get my permissions back.

我在 Windows 中遇到了权限问题,必须这样做icacls containingFolder /reset /t /l /c,然后双击该文件夹才能恢复我的权限。