git 远程添加其他 SSH 端口
在 Git 中,当我的主机使用不同的 SSH 端口时,如何添加远程源服务器?
git remote add origin ssh://user@host/srv/git/example
In Git, how can I add a remote origin server when my host uses a different SSH port?
git remote add origin ssh://user@host/srv/git/example
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
你可以这样做:
1234
是正在使用的ssh端口You can just do this:
1234
is the ssh port being used您需要编辑 ~/.ssh/config 文件。添加如下内容:
快速谷歌搜索显示 很少 不同的资源比我更详细地解释了它。
You need to edit your ~/.ssh/config file. Add something like the following:
A quick google search shows a few different resources that explain it in more detail than me.
您可以继续使用传统的 URL 形式通过 SSH 访问 git,而不是使用
ssh://
协议前缀,只需进行一点小小的更改。提醒一下,常规 URL is:要指定替代端口,请将
user@host
部分放在括号中,包括端口:但如果端口更改只是暂时的,您可以告诉 git 使用不同的 SSH命令而不是更改存储库的远程 URL:
Rather than using the
ssh://
protocol prefix, you can continue using the conventional URL form for accessing git over SSH, with one small change. As a reminder, the conventional URL is:To specify an alternative port, put brackets around the
user@host
part, including the port:But if the port change is merely temporary, you can tell git to use a different SSH command instead of changing your repository’s remote URL:
最佳答案对我不起作用。我从一开始就需要 ssh:// 。
Best answer doesn't work for me. I needed
ssh://
from the beggining.对于那些编辑
./.git/config
的人For those of you editing the
./.git/config
对于 gitlab,示例 ssh 端口是 2224,因此:
for gitlab, example ssh port is 2224, therefore:
尝试连接到我的 git 服务器时遇到类似的问题
(有一个 gitea docker 容器中的服务器,ssh 端口配置为
2022
,而不是标准 22,此处作为示例my-git-server.lan
)。(这将创建两个文件:公钥
mykeyfile.pub
和私钥mykeyfile
,不带任何扩展名)(请注意,用户名始终
git
,无论您在 git 服务器上的实际用户名是什么).. 使用 -v“详细模式”来分析任何错误:(
再次注意,用户名是 always
git
)ssh://[email] protected]:2022/alex/myproject.git
用于本地 git 存储库(再次注意用户 git 和端口 2022), ..检查远程配置(在这里您还可以看到在我的 git 服务器上我的实际用户是alex,存储库是myproject)
完成!您现在可以使用 git-server .. fetch/commit/push 等
(这是我在 serverfault.com 上发布的帖子的副本 )
更新:正如评论中正确指出的 - 您不一定需要在远程 URL 中指定端口 2022,因为它已经在 < code>~/.ssh/config 文件为
PORT 2022
。Had a similar issue trying to connect to my git server
( have a gitea server in a docker container with ssh-port configured to
2022
, instead of standard 22, here as an examplemy-git-server.lan
).(this will create two files: public-key
mykeyfile.pub
and private-keymykeyfile
without any extension)(notice that the username is always
git
, regardless of your actual username on your git-server).. use -v "verbose mode" to analyse any errors:
(again, notice that the username is always
git
)ssh://[email protected]:2022/alex/myproject.git
for your local git repository (again, notice the user git and the port 2022), .. check remote configuration( here you also see that on my git-server my actual user is alex and repository is myproject )
Done! You can now work with your git-server .. fetch/commit/push etc.
( this is a copy of my post on serverfault.com )
Update: as rightly noted in the comments - you do not necessarily need to specify port 2022 in the remote-url, since it is already configured in
~/.ssh/config
file asPORT 2022
.只需看看如何正确设置 ~/.ssh/config 文件即可。
您可以轻松地为不同的主机指定不同的设置。
要解决您的问题,您可以
查看 ssh_config 手册页,它在前几页上解释了您需要了解的所有内容。
Just have a look at how to set up your ~/.ssh/config file correctly.
You can specify different settings for different hosts easily.
To solve your problem you would set
Have a look at the ssh_config manual page, it explains everything you need to know on the first few pages.
1.git remote add ${shortname} ${url}
2.git remote remove Shortname (删除远程)
3.git remote -v(就是查看你当前的远程列表)
4.git push 远程分支
5.git remote rename AB(将 A 重命名为 B)
6.git remote 显示短名称(显示远程信息)
所有这些都适合我。
1.git remote add ${shortname} ${url}
2.git remote remove shortname (is remove a remote)
3.git remote -v (is to see your current remote list)
4.git push remote branch
5.git remote rename A B (rename A to B)
6.git remote show shortname (show remote info)
All this works for me.