Git 无法使用 Ubuntu → Windows Samba 共享目录

Git not working with Ubuntu → Windows Samba shared directory

我正在 运行 设置我的 git 存储库的 Virtual Box Ubuntu 虚拟机。然后我使用 Samba 创建了一个共享文件夹,并在 smb.conf

中进行了以下配置
path = /home/allan/git_repo
valid users = allan
read only = no
hide dot files = no

当我导航到适当的网络位置 (\192.168.65.101\git_repo) 时,我在那里看到了我的所有文件和文件夹,包括我的 .git 文件夹,它似乎包含我需要的一切。

如果我在 bash 终端 (MINGW64) 中导航到网络位置,我发现它未被识别为 git 存储库。像 git rev-parse HEAD returns fatal: Not a git repository (or any of the parent directories): .git 这样的简单命令 另一方面,向下导航子目录级别会导致某种识别(例如,我可以 运行 git rev-parse HEAD 并获得有效响应)。虽然有很多功能我需要位于上层目录,所以我很困惑发生了什么以及如何解决这个问题。

确保不要在 .git 文件夹中执行您的 git 命令。

如果应该在 .git/ 的父文件夹中执行。

此外,请确保使用 latest version of Git for Windows,因为它更好地支持 \... 路径。
另见“”。


Git 2.24(2019 年第 4 季度)将认识到,在 Windows 上,现在允许像使​​用任何其他目录一样使用 UNC 共享的根级别。

参见 commit 5cf7b3b, commit e2683d5, commit d17f212 (24 Aug 2019) by Johannes Schindelin (dscho)
(由 Junio C Hamano -- gitster -- in commit b57a88a 合并,2019 年 9 月 30 日)

setup_git_directory(): handle UNC root paths correctly

When working in the root directory of a file share (this is only possible in Git Bash and Powershell, but not in CMD), the current directory is reported without a trailing slash.

This is different from Unix and standard Windows directories: both / and C:\ are reported with a trailing slash as current directories.

If a Git worktree is located there, Git is not quite prepared for that:
while it does manage to find the .git directory/file, it returns as length of the top-level directory's path one more than the length of the current directory, and setup_git_directory_gently() would then return an undefined string as prefix.

In practice, this undefined string usually points to NUL bytes, and does not cause much harm. Under rare circumstances that are really involved to reproduce (and not reliably so), the reported prefix could be a suffix string of Git's exec path, though.

更具体地说,git-for-windows/git issue 1320 报告 git status 在 Windows 共享的根 UNC 路径中失败。

fatal: Not a git repository (or any of the parent directories): .git

Fix .git/ discovery at the root of UNC shares

A very common assumption in Git's source code base is that offset_1st_component() returns either 0 for relative paths, or 1 for absolute paths that start with a slash.
In other words, the return value is either 0 or points just after the dir separator.

This assumption is not fulfilled when calling offset_1st_component() e.g. on UNC paths on Windows, e.g. "//my-server/my-share".
In this case, offset_1st_component() returns the length of the entire string (which is correct, because stripping the last "component" would not result in a valid directory), yet the return value still does not point just after a dir separator.

This assumption is most prominently seen in the setup_git_directory_gently_1() function, where we want to append a ".git" component and simply assume that there is already a dir separator.
In the UNC example given above, this assumption is incorrect.

As a consequence, Git will fail to handle a worktree at the top of a UNC share correctly.

Let's fix this by adding a dir separator specifically for that case: we found that there is no first component in the path and it does not end in a dir separator? Then add it.