Git 存储库 URL - 没有绝对路径的 SSH 语法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14348874/
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
Git repository URL - SSH syntax without absolute path
提问by Neo
I'm running a git repository server accessible via SSH, on a custom port (let's say 12345). I have found that in this case the repository URLs need to be specified using SSH syntax:
我正在一个自定义端口(比如 12345)上运行一个可通过 SSH 访问的 git 存储库服务器。我发现在这种情况下,需要使用 SSH 语法指定存储库 URL:
git clone ssh://[email protected]:12345/absolute/path/to/repository
I'd like to setup it in such a way that it would be possible for users to clone repositories without specifying the absolutepath.
我想以这样一种方式设置它,即用户可以在不指定绝对路径的情况下克隆存储库。
git clone ssh://[email protected]:12345/repository.git
I have researched the topic and found the following options:
我研究了这个话题,发现了以下选项:
- Remove the custom port from the URL and make the users add it to their
~/.ssh/config
file (a last-resort workaround rather than a solution). - Drop SSH and use
git-daemon
and specify its--base-path
parameter (still a workaround...) - Use an utility like
gitosis
orgitolite
- but wouldn't it be an overkill to use it for this purpose only? Gitosis is discontinued, gitolite is a quite huge piece of software...
- 从 URL 中删除自定义端口并让用户将其添加到他们的
~/.ssh/config
文件中(最后的解决方法而不是解决方案)。 - 删除 SSH 并使用
git-daemon
并指定其--base-path
参数(仍然是一种解决方法......) - 使用像
gitosis
或这样的实用程序gitolite
- 但仅将其用于此目的会不会有点矫枉过正?Gitosis 停产了,gitolite 是一个相当大的软件......
All I want is a "prettier" SSH URL with custom port (so I can distribute the whole git clone
command and require nothing more from the remote user). Which one of the above solutions would be the most viable in my case?
我想要的只是一个带有自定义端口的“更漂亮”的 SSH URL(这样我就可以分发整个git clone
命令并且不需要来自远程用户的更多信息)。在我的情况下,上述哪一种解决方案最可行?
回答by larsks
If you use the alternate form of ssh
URLs you don't need an absolute path. For example...
如果您使用ssh
URL的替代形式,则不需要绝对路径。例如...
git clone [email protected]:repos/myrepo.git
...will clone repository repos/myrepo.git
relative to my home directory, although this doesn't permit use of an alternate port. However, you can also use ~
in either form to indicate the user's home directory, e.g.:
...将克隆repos/myrepo.git
相对于我的主目录的存储库,尽管这不允许使用备用端口。但是,您也可以~
以任何一种形式使用来指示用户的主目录,例如:
git clone ssh://[email protected]:12345/~/repository.git
Incidentally, despite being discontinued, gitosis
functions quite well, and the code is both small and easy to understand. It offers a useful set of access controls and self-service management of repositories. I wouldn't discount it completely.
顺便说一下,虽然停产了,但gitosis
功能还是不错的,代码又小又容易理解。它提供了一组有用的访问控制和存储库的自助管理。我不会完全打折。
回答by user1708042
I do this, not exactly what you asked, but close and with prettier links:
我这样做,不是你问的那样,而是关闭并提供更漂亮的链接:
create a path like
创建一个类似的路径
/srv/git
where you place you git projects
你把你的 git 项目放在哪里
next make a symbolic link to home:
接下来做一个到家的符号链接:
ln -s /srv/git $HOME
next you can have shorter links
接下来你可以有更短的链接
git clone user@server:git/myproject.git
if you have a single project you can get rid of the git/ part in the url
如果你有一个项目,你可以去掉 url 中的 git/ 部分
ln -s /srv/git/myproject.git $HOME
next
下一个
git clone user@server:myproject.git
will work. Of course the .git at the end of the URL comes only from creating the bare with .git in the name. Note also that ssh:// part is not needed: the @ implies it is an ssh URL for git.
将工作。当然,URL 末尾的 .git 仅来自创建名称中带有 .git 的裸。另请注意,不需要 ssh:// 部分:@ 表示它是 git 的 ssh URL。
Updated: let me add a sidenote: following the most recent Filesystem Hierarchy StandardI use now /srv/git
as repository location.
更新:让我添加一个旁注:遵循我现在用作存储库位置的最新文件系统层次结构标准/srv/git
。
回答by Eric Anderson
I'm not sure this really deserves to be a whole answer, but I'm sick of fighting with comment-specific formatting rules. I come to say:
我不确定这是否真的值得一个完整的答案,但我厌倦了与特定于评论的格式规则作斗争。我来说:
I just want to add that this seems to be a version-specific change, though I'm not sure if it's client-side or server/repo-holder side: I have a .git/config
file with lots of URLs like:
我只想补充一点,这似乎是特定于版本的更改,但我不确定它是客户端还是服务器/存储库持有者端:我有一个.git/config
包含很多 URL的文件,例如:
[remote "bob"]
url = ssh://ME@MACHINE<strong>/~ME</strong>/repositories/REPO.git
fetch = +refs/heads/*:refs/remotes/bob/*
They all worked when I set them up ... in 2010, and were still working the last time I used those remotes, in 2016 or so.
当我在 2010 年设置它们时,它们都可以工作,并且在 2016 年左右我上次使用这些遥控器时仍在工作。
Now, on version 2.18.0, those don't work, and I need to use the :/~ME/foo
or :/~/foo
notation. Which is to say that what you're trying to do no hasn't always been wrong.
现在,在 2.18.0 版本上,那些不起作用,我需要使用:/~ME/foo
or:/~/foo
符号。也就是说,您尝试做的事情并不总是错误的。