英文:
Git rev-parse --show-toplevel not working for worktree within WSL
问题
我正在使用git工作树,并希望在Windows 11
和WSLv2(Ubuntu 20)
中工作。有一个使用git rev-parse --show-toplevel
来确定存储库根目录的makefile。
然而,在WSL上,makefile会受到以下行为的影响:
在Windows上
git rev-parse --show-toplevel
=> D:/Repos/MyRepo-0718
当我切换到WSL时
git rev-parse --show-toplevel
=> fatal: not a git repository: /mnt/d/Repos/MyRepo-0718/D:/Repos/MyRepo/.git/worktrees/MyRepo-0718
我在Windows和Ubuntu上都有最新版本的GIT。
有办法让--show-toplevel
在WSL上产生相同的输出吗?
英文:
I am using git worktrees and want to work in Windows 11
and WSLv2 (Ubuntu 20)
. Have a makefile that uses git rev-parse --show-toplevel
to determine the repository root.
However, on WSL the makefile is tripped by the following behavior:
On Windows
git rev-parse --show-toplevel
=> D:/Repos/MyRepo-0718
When I switch to WSL
git rev-parse --show-toplevel
=> fatal: not a git repository: /mnt/d/Repos/MyRepo-0718/D:/Repos/MyRepo/.git/worktrees/MyRepo-0718
I have the latest versions of GIT on both Windows and Ubuntu.
Is there a way to make --show-toplevel
produce the same output on WSL?
答案1
得分: 2
在一个工作树中,通常没有.git
目录。相反,你会有一个.git
文件,它被称为git链接,引用了主存储库内部的一个特殊目录。它通过路径来进行这个引用。
如果你在Windows上创建这个工作树,它会嵌入一个Windows路径。然而,Linux上的Git不了解Windows路径,或者说,它不了解WSL,因为该发行版完全在一个带有Unix路径的Unix系统上开发。
你可以尝试非常谨慎地编辑工作树中.git
文件中的路径,以使用相对路径,这应该可以工作。
然而,一般来说,使用绝对路径,这是默认设置,是不会工作的,因为Linux上的Git永远不会接受Windows路径(因为它会将其视为一个只包含冒号的相对路径),而Windows上的Git也不会接受WSL路径(因为Git for Windows和WSL在Unix样式路径的根上使用了不同的根)。
你可以在Git列表中请求一个相对路径选项作为一个新功能(或者自己发送补丁),但在目前的工具状态下,这是不可能做到的。
英文:
In a worktree, you typically don't have a .git
directory. Instead, you have a .git
file, which is called a gitlink and refers to a special directory inside the main repository. It makes this reference via a path.
If you make this worktree on Windows, then it embeds a Windows path. However, a Linux Git doesn't know about Windows paths, or, for that matter, WSL, since the distro is developed entirely on a Unix system with Unix paths.
You can try, very carefully, to edit the path in the .git
file in the worktree to use a relative path, which should work.
However, in general, using an absolute path, which is the default, will not work, because a Linux Git will never accept a Windows path (since it will consider it a relative path that just happens to contain a colon) and a Windows Git will not accept a WSL path (since Git for Windows and WSL use different roots for their Unix-style paths).
You could request a relative path option on the Git list as a new feature (or send the patch yourself), but it isn't possible to do so with the tooling as it stands.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论