在家时,git fetch origin master
应该通过 ssh 连接到 git@192.168.xx.xx
。
在路上时,git fetch origin master
应该通过 ssh 连接到 git@xxx.linkpc.net
。
可能的解决方案
- Having multiple remotes对于相同的存储库/分支工作,但 git 不必要地跟踪多个头。这种解决方案只会破坏美观。
- 将主机名分配给远程和注释/取消注释
/etc/hosts
条目以委托(delegate) ips 是一个 nice solution但包括 sudo 和输入 root 密码,这有点乏味。 - 每个用户的主机文件是 out of the question.
- 编写一个脚本,每次调用
origin
时都会委托(delegate)正确的 ip,is ideal . - 编写一个脚本,每次用户调用它时都会以 root 身份执行,这似乎是一个 marginally fair solution .
问题
如何使用无论如何都不应被视为“hackish character”的解决方案来解决这个问题?
最佳答案
请记住,您正在通过 SSH 建立隧道,因此您的 ~/.ssh/config
将每个用户的主机文件带回问题中,例如,
Host dynamic-repo-host
HostName 192.168.xx.xx
#HostName xxx.linkpc.net
然后修改origin
的URL为
ssh config remote.origin.url git@dynamic-repo-host
根据需要手动修改~/.ssh/config
,然后给出你想要的效果。
如果你想要一个完全不干涉的解决方案,请考虑在 ~/.ssh/config
中使用 Match
,但要执行的具体命令将取决于您的本地计算机运行的网络。
关于linux - Git - SSH - 主机 : How can I delegate different IPs to remote origin, 取决于每次工作的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25703324/