Git拒绝重置/丢弃文件

mgP*_*ePe 75 git git-status

我有一个项目与某些js文件,我无法更新.我在本地运行OSX,而​​我的远程/临时服务器是Linux(CentOS).

在本地克隆我的项目后,我注意到我有所有那些具有git状态的文件modified.我从来没有修改它们,所以我尝试了discard changes或者reset它们,但它们再次出现.修改中的更改是删除所有行并再次添加它们.

我不确定为什么会发生这种情况或如何解决它,以便我的git状态是干净的,因为它需要.

以下是git状态的几行:

#   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
Run Code Online (Sandbox Code Playgroud)

更新1:

我现在设法提交上述文件,但登台服务器被锁定,因为它不会提取新的编辑:

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
Run Code Online (Sandbox Code Playgroud)

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

Updates were rejected because a pushed branch tip is behind its remote counterpart
Run Code Online (Sandbox Code Playgroud)

我试过了:

git reset --hard
Run Code Online (Sandbox Code Playgroud)

git stash
git stash drop
Run Code Online (Sandbox Code Playgroud)

但它们不起作用,没有任何反应.

更新2:

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.
...
Run Code Online (Sandbox Code Playgroud)

AD7*_*six 105

规范化行尾

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

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

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

git config --global core.autocrlf input
Run Code Online (Sandbox Code Playgroud)

然后提交行结束规范化:

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
Run Code Online (Sandbox Code Playgroud)

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

解决linux-machine冲突

登台服务器已锁定,因为它不会进行新的编辑.

错误消息显示"你的本地下列文件的更改将被合并覆盖",这意味着它们含有要么继续之前被提交或丢弃的局部变化.假设登台服务器正常使用(它没有任何有意的更改),可以放弃本地更改.例如,执行以下操作:

$ git fetch origin
# Retrieve updates

$ git reset --hard origin/master
# Forcibly change the current branch to match origin/master
Run Code Online (Sandbox Code Playgroud)

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


Von*_*onC 14

我总是提到确保你core.autocrlf的设置false,如" Git:在crlf规范化后使用存储卡住了回购? "

git config --global core.autocrlf false
Run Code Online (Sandbox Code Playgroud)

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


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

我在Mac上,这个模糊的配置更改似乎解决了我没有关于无阶段更改的困境.

git config --global core.trustctime false
Run Code Online (Sandbox Code Playgroud)

  • 好建议。如果没有 Windows 用户协作以换行,或者如果问题仅与供应商文件有关,则最合适。但是,如果有 _is_ Windows + mac/linux 用户的混合团队 - 这将导致永久的空白更改,具体取决于谁最后触摸了文件。+1。 (2认同)

Nao*_*Bar 9

我只是花了2个小时(!)在同一个问题上使用.svg文件(标量矢量图形),在没有我介入情况下,在"恢复"之后不断变化.

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

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

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

太容易了,我哭了!


Kre*_*uvf 5

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

#!/bin/sh

# Use in root dir of git repository
# Fixes some newline-related weirdness
git rm --cached -r .
git reset --hard
Run Code Online (Sandbox Code Playgroud)

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

# Auto detect text files and perform LF normalization
* text=auto
Run Code Online (Sandbox Code Playgroud)

之后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")
Run Code Online (Sandbox Code Playgroud)

解决方案是* 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")
Run Code Online (Sandbox Code Playgroud)

上的更改.gitattributes可以被丢弃,Git 仍然会得到满足。

编辑 +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")
Run Code Online (Sandbox Code Playgroud)

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