Git 拒绝重置/丢弃文件

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

Git refuses to reset/discard files

gitgit-status

提问by mgPePe

I have a project with certain js files which I cannot update. I run OSX locally and my remote/staging server is Linux (CentOS).

我有一个项目,其中包含无法更新的某些 js 文件。我在本地运行 OSX,我的远程/登台服务器是 Linux (CentOS)。

Right after cloning my project locally, i noticed I have all those files with git status modified. I never modified them, so I tried to discard changesor resetthem, but they come up again. The change that is in the modification is deleting all lines and adding them again.

在本地克隆我的项目后,我注意到所有这些文件都带有 git status modified。我从来没有修改过它们,所以我试图discard changesreset它们,但它们又出现了。修改中的更改是删除所有行并再次添加它们。

I'm not sure why this happens or how to fix it so that my git status is clean as it needs to be.

我不确定为什么会发生这种情况或如何解决它,以便我的 git 状态按照需要保持干净。

Here is a few lines from the git status:

这是 git status 中的几行:

#   modified:   app/webroot/js/ckeditor/plugins/devtools/lang/el.js
#   modified:   app/webroot/js/ckeditor/plugins/devtools/lang/fa.js
#   modified:   app/webroot/js/ckeditor/plugins/devtools/lang/gu.js

UPDATE 1:

更新1:

I have now managed to commit the above files, but the staging server is locked because it won't pull new edits:

我现在已经设法提交了上述文件,但临时服务器被锁定,因为它不会拉新的编辑:

error: Your local changes to the following files would be overwritten by merge:
    app/webroot/js/ckeditor/_source/lang/ar.js
    app/webroot/js/ckeditor/_source/lang/bg.js
    app/webroot/js/ckeditor/_source/lang/bn.js
    app/webroot/js/ckeditor/_source/lang/cs.js
    ...
Aborting

I can't commit/push because:

我无法提交/推送,因为:

Updates were rejected because a pushed branch tip is behind its remote counterpart

I tried:

我试过:

git reset --hard

and

git stash
git stash drop

But they don't work, nothing happens.

但它们不起作用,什么也没有发生。

UPDATE 2:

更新 2:

git diffgives me:

git diff给我:

The file will have its original line endings in your working directory.
warning: CRLF will be replaced by LF in app/webroot/js/ckeditor/_source/lang/fa.js.
The file will have its original line endings in your working directory.
warning: CRLF will be replaced by LF in app/webroot/js/ckeditor/_source/lang/gu.js.
The file will have its original line endings in your working directory.
...

回答by AD7six

Normalize line endings

规范化行尾

The change that is in the modification is deleting all lines and adding them again.

修改中的更改是删除所有行并再次添加它们。

This is because the newlines are being changed between the committed files and the files on disk.

这是因为已提交的文件和磁盘上的文件之间的换行符正在更改。

Github has a handy pagedetailing how to deal with this kind of problem, in brief (for linux/OSX), step one is to change your git config so it sorts out the line endings for you:

Github 有一个方便的页面,详细说明了如何处理此类问题,简而言之(对于 linux/OSX),第一步是更改您的 git 配置,以便为您整理行尾:

git config --global core.autocrlf input

Then commit line-endings normalization:

然后提交行尾规范化:

git rm --cached -r .
# Remove everything from the index.

git reset --hard
# Write both the index and working directory from git's database.

git add .
# Prepare to make a commit by staging all the files that will get normalized.
# This is your chance to inspect which files were never normalized. You should
# get lots of messages like: "warning: CRLF will be replaced by LF in file."

git commit -m "Normalize line endings"
# Commit

And then, line endings should be handled correctly. See the help page on github for more information, or the relevant section of the git docs formatting and whitespace.

然后,应正确处理行尾。有关更多信息,请参阅 github 上的帮助页面,或 git docs格式和空白的相关部分。

Resolving linux-machine conflicts

解决linux-机器冲突

the staging server is locked because it won't pull new edits.

临时服务器被锁定,因为它不会提取新的编辑。

The error message reads "Your local changes to the following files would be overwritten by merge:", that means they contain local changes which should either be committed or discarded before continuing. Assuming normal usage for the staging server (it doesn't have any intentional changes) the local changes can be discarded. For example do the following:

错误消息显示“您对以下文件的本地更改将被合并覆盖:”,这意味着它们包含在继续之前应提交或丢弃的本地更改。假设临时服务器的正常使用(它没有任何有意的更改),本地更改可以被丢弃。例如执行以下操作:

$ git fetch origin
# Retrieve updates

$ git reset --hard origin/master
# Forcibly change the current branch to match origin/master

This will retrieve the repository history, without updating the working copy, and then update to exactly match the master branch in the repository. Note that the last command will discard all uncommitted changes.

这将检索存储库历史记录,而不更新工作副本,然后更新以完全匹配存储库中的主分支。请注意,最后一个命令将丢弃所有未提交的更改。

回答by VonC

I always mentioned making sure that your core.autocrlfis set to false, as in "Git : stuck repo using stash after crlf normalization?"

我总是提到确保您core.autocrlf的设置为false,如“ Git:在 crlf 规范化后使用 stash 卡住了 repo?

git config --global core.autocrlf false

Also make sure that you don't have a .gitattributesfile with eoldirectives that would try to convert the end-of-lines.
The basic idea is: do you still see that error message when you make sure there is noautomatic conversion of any sort?

还要确保您没有包含试图转换行尾的指令的.gitattributes文件eol
基本思想是:当您确定没有任何类型的自动转换时,您是否仍然看到该错误消息?



But just in case, consider also "Git rebase fails, 'Your local changes to the following files would be overwritten by merge'. No local changes?"

但为了以防万一,还要考虑“ Git rebase 失败,'您对以下文件的本地更改将被合并覆盖'。没有本地更改?

I'm on a mac, and this obscure config change seemed to fix all my woes regarding unstaged changes when there were none.

我在 Mac 上,这个晦涩的配置更改似乎解决了我所有关于未暂存更改的问题。

git config --global core.trustctime false

回答by Naor Bar

I just spent 2 hours (!) on the same issue with a .svg file (Scalar Vector Graphics), which kept changing after 'revert'without my intervention.

我刚刚用 .svg 文件(标量矢量图形)在同一问题上花了 2 个小时(!),在没有我干预情况下,它在“恢复”后不断变化。

So the file shows up as modified in 'git status'; reverting it succeeds, but it keeps changing, so the 'pull'is failing again and again.... so annoying!

因此该文件在'git status' 中显示为已修改;恢复它成功,但它一直在变化,所以“拉”一次又一次地失败......太烦人了!

No luck with 'git reset', 'git ignore', 'git untrack'etc...

没有运气'git reset''git ignore''git untrack'等......

Finally, I solved it by removing the file from my local system (not 'git delete', just Shift + Delete) >> now 'pull'request passes, and the file is fetched from the remote repository.

最后,我通过从我的本地系统中删除文件(不是'git delete',只是 Shift + Delete)解决了这个问题 >> 现在'pull'请求通过,并且文件是从远程存储库中获取的。

So easy, I could cry!

太简单了,我哭了!

回答by Kreuvf

This issue repeatedly popped up with the Roll20 character sheets repository on an Ubuntu machine and I could solve it by

这个问题在 Ubuntu 机器上的 Roll20 字符表存储库中反复出现,我可以通过

#!/bin/sh

# Use in root dir of git repository
# Fixes some newline-related weirdness
git rm --cached -r .
git reset --hard

But, this stopped resolving the issue completely today and by looking around Stack Overflow I found the culprit to be their .gitattributesfile:

但是,今天这完全停止了解决问题,通过查看 Stack Overflow,我发现罪魁祸首是他们的.gitattributes文件:

# Auto detect text files and perform LF normalization
* text=auto

After git pull origin master, git statusreturned:

之后git pull origin mastergit status返回:

On branch master
Your branch is up-to-date with 'origin/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:   Star Wars Revised RPG/SWRRPG-updated.html

no changes added to commit (use "git add" and/or "git commit -a")

The solution was to remove the * text=autoline from the .gitattributes:

解决方案是* text=auto从 .gitattributes 中删除该行:

$ git status
On branch master
Your branch is up-to-date with 'origin/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:   .gitattributes

no changes added to commit (use "git add" and/or "git commit -a")

The changes on .gitattributescan be discarded and Git will still be satisfied.

上的更改.gitattributes可以丢弃,Git 仍将满足。

Edit +1d: Retried the .gitattributes "trick" today, but did not git statusbefore discarding the .gitattributes changes. For no reason obvious to me (maybe caching of git status?), a git statusafterwards returned this again:

编辑 +1d:今天重试了 .gitattributes “技巧”,但git status在丢弃 .gitattributes 更改之前没有。无缘无故对我来说很明显(也许缓存git status?),git status之后又返回了这个:

On branch master
Your branch is up-to-date with 'origin/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:   Star Wars Revised RPG/SWRRPG-updated.html

no changes added to commit (use "git add" and/or "git commit -a")

Doing it again, but with git statusinbetween worked.

再做一次,但git status中间起作用了。