git & Cygwin - 尾随空格导致“未更新”

发布于 2024-08-07 22:44:27 字数 1363 浏览 4 评论 0原文

Windows 上使用 Cygwin 的 Git 充满了危险。然而,有一个真的开始让我烦恼。

它与 core.autocrlf=true 行为有关。在花了一周的时间在网上搜寻之后,很明显,您将遇到的问题并没有那么严重。但是,如果文件末尾有一行尾随空格,则似乎会产生重大问题。

问题是 git 认为该文件有本地修改,尽管事实并非如此。例如,在全新克隆之后,“git status”或“git diff”将立即显示任何此类文件的修改情况。 “git reset --hard”可以完成它的工作,但这些文件仍然显示为已修改。 'git diff' 将差异显示为“删除了一个空行,添加了一个空行”。

问题是,这会阻止 git pull!

$ git pull
Updating 73bcc56..dba6253
error: Entry 'foo.py' not uptodate. Cannot merge.
$ git reset --hard
HEAD is now at 73bcc56 ...
$ git diff
diff --git a/foo.py b/foo.py
index 4cc3854..ccde3f6 100644
--- a/foo.py
+++ b/foo.py
@@ -14,7 +14,7 @@ class TestHelpFunctions(unittest.TestCase):
     def testVersion(self):
         v = sendCommand("version")
         self.assertEqual(len(v), 2)
-
+

好吧,我想——方式上有一个局部的改变。如果我把它藏起来怎么办?不 - 在 git stash 之后,它仍然显示该文件是本地更改的。

好吧,如果我添加呢?当然,这仍然会阻止拉动:

$ git add foo.py
$ git pull
Updating 73bcc56..dba6253
error: Entry 'foo.py' would be overwritten by merge. Cannot merge.

好吧,最后一次尝试 - 让我们提交它并完成它。哦,太棒了,这个文件有冲突。但令人惊奇的是,该文件没有冲突标记!不幸的是,“git pull”现在抱怨我正处于冲突合并之中,即使没有标记冲突。

我发现,解决方法是永远不要提交换行符之前带有尾随空格的文件。它只会导致 git 认为文件被修改,但实际上并没有,可能是由于 DOS-UNIX 行结束逻辑明显被破坏。

不管怎样,我并不是真的在寻找答案,因为最后我只是用核武器摧毁了整个事情并重新克隆。我真正想知道的是,人们在尝试将 git 与 Cygwin 结合使用时如何保持理智。

当目录名为“FOO”时,不要让我开始使用“git add foo/a foo/b FOO/c”...

Git on Windows w/ Cygwin is fraught with dangers. However there's one that's really starting to bug me.

It's related to the core.autocrlf=true behaviour. After spending a week trawling the 'net it became clear that the problems you'll encounter are less bad with this set. However, if a file has a line with trailing whitespace on the end, it appears to create a major problem.

The problem is that git thinks the file has local modifications, even though it does not. For example, after a brand-new fresh clone, a 'git status' or 'git diff' will immediately show any such files as modified. A 'git reset --hard' does its thing, but then those files still show as modified. 'git diff' shows the differences as being "an empty line removed, an empty line added".

The problem is, this blocks a git pull!

$ git pull
Updating 73bcc56..dba6253
error: Entry 'foo.py' not uptodate. Cannot merge.
$ git reset --hard
HEAD is now at 73bcc56 ...
$ git diff
diff --git a/foo.py b/foo.py
index 4cc3854..ccde3f6 100644
--- a/foo.py
+++ b/foo.py
@@ -14,7 +14,7 @@ class TestHelpFunctions(unittest.TestCase):
     def testVersion(self):
         v = sendCommand("version")
         self.assertEqual(len(v), 2)
-
+

Ok, I think - there's a local change in the way. What if I stash it? Nope - after git stash it still shows this file as locally changed.

Ok, what if I add it? Of course this still blocks a pull:

$ git add foo.py
$ git pull
Updating 73bcc56..dba6253
error: Entry 'foo.py' would be overwritten by merge. Cannot merge.

Ok, one last try - let's just commit it and be done with it. Oh great it's conflicted on this file. But the amazing thing is, the file has no conflict markers! Unfortunately 'git pull' now complains that I'm in the middle of a conflicted merge, even though there's no conflict marked.

The fix, I've discovered, is to never commit a file with trailing whitespace before a newline. It just causes git to think the file is modified when it's not, probably due to the DOS-UNIX line ending logic that's obviously broken.

Anyway, I'm not really looking for an answer, because in the end I just nuked the entire thing and recloned. What I'd really like to know is how anyone maintains their sanity while trying to use git with Cygwin.

Don't get me started on 'git add foo/a foo/b FOO/c' when the directory is called "FOO"...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

破晓 2024-08-14 22:44:27

根据我的经验,我的所有文件都使用 UNIX eol 样式,并且我始终将 core.autocrlf 设置为 false。
请参阅此SO答案

发生的任何事情“自动”,特别是在分布式环境中(您无法保证远程存储库上的相同设置)是自找麻烦。

基本上,如果您的工具(例如 使用对于合并)设置正确,它们将处理任何空间/终止问题。不是直接使用 Git。

In my experience, all my files use the UNIX eol style, and I have always set the core.autocrlf to false.
See this SO answer

Anything which happens "automagically", especially in a distributed environment (where you cannot guarantee the same setup on remote repositories) is asking for trouble.

Basically, if your tools (like the one used for merge) are set correctly, they will handle any space/eol issue. Not Git directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文