如何指定在git中使用哪个SSH密钥进行git push以获得作为镜像的gitorious? [英] How to specify which SSH key to use within git for git push in order to have gitorious as a mirror?

查看:866
本文介绍了如何指定在git中使用哪个SSH密钥进行git push以获得作为镜像的gitorious?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个托管在git.debian.org(alioth)上的项目,我想配置一个post-receive钩子来更新 http://gitorious.org

I have a project hosted on git.debian.org (alioth) and I'd like to configure a post-receive hook to update a mirror of the repository on http://gitorious.org

我想我必须使用 git push - 镜像gitorious

现在,我需要让Alioth授权才能成功推送。我该怎么做?

Now, I'll need to have Alioth authorized on gitorious for the push to succeed. How do I do that?

我想我需要在gitorious上配置一个用户并为它创建一个ssh密钥。然后当我在post-receive钩子中执行git push时,确保使用这个ssh键。

I suppose I need to configure a user on gitorious and create a ssh key for it. And then when I do the git push in the post-receive hook, make sure this ssh key is used.

我可以使用〜/ .ssh / config 但问题在于许多用户可能会推送alioth,而每个人都必须登录并配置〜/ .ssh / config 。相反,我想有一个命令行选项或环境变量来告诉ssh使用哪个键。我能做到吗?

I could use a ~/.ssh/config but the problem is that many users can push on alioth, and everyone would have to log in and configure the ~/.ssh/config. Instead, I'd like to have a command line option or an environment variable to tell ssh which key to use. Can I do that?

另外,您是否有其他想法可以实现镜像?而且,是否有可能通过其他方式来配置它(gitorious push alioth)?

Also, do you have other ideas how mirroring can be achieved? And, is it possible to configure it the other way around (gitorious pushing on alioth)?

推荐答案

答案可以找到在 git参考手册中。


GIT_SSH



如果设置了这个环境变量,当他们需要连接到远程系统时,git fetch和git push会使用这个命令而不是ssh。 $ GIT_SSH 命令将只有两个参数:URL中的username @ host(或主机)和要在该远程系统上执行的shell命令。

GIT_SSH

If this environment variable is set then git fetch and git push will use this command instead of ssh when they need to connect to a remote system. The $GIT_SSH command will be given exactly two arguments: the username@host (or just host) from the URL and the shell command to execute on that remote system.

要将选项传递给您想列在 GIT_SSH 中的程序,您需要将程序和选项到一个shell脚本中,然后设置 GIT_SSH 来引用shell脚本。

To pass options to the program that you want to list in GIT_SSH you will need to wrap the program and options into a shell script, then set GIT_SSH to refer to the shell script.

通常它是通过您的个人 .ssh / config 文件更容易地配置任何所需的选项。

Usually it is easier to configure any desired options through your personal .ssh/config file. Please consult your ssh documentation for further details.

所以,我需要编写一个包装脚本,我写这个 push-gitorious.sh 脚本:

So, I need to write a wrapper script, I write this push-gitorious.sh script:

#!/bin/sh


if [ "run" != "$1" ]; then
  exec ssh -i "$GITORIOUS_IDENTITY_FILE" -o "StrictHostKeyChecking no" "$@"
fi

remote=YOUR_SSH_GITORIOUS_URL

echo "Mirroring to $remote"

export GITORIOUS_IDENTITY_FILE="`mktemp /tmp/tmp.XXXXXXXXXX`"
export GIT_SSH="$0"

cat >"$GITORIOUS_IDENTITY_FILE" <<EOF
YOUR SSH PRIVATE KEY

EOF
cat >"$GITORIOUS_IDENTITY_FILE.pub" <<EOF
YOUR SSH PUBLIC KEY

EOF

#echo git push --mirror "$remote"
git push --mirror "$remote"

rm -f "$GITORIOUS_IDENTITY_FILE"
rm -f "$GITORIOUS_IDENTITY_FILE.pub"

exit 0

当然,您必须填写私钥(公钥只包含在脚本中,仅供参考,您还需要填写有价值的URL。

Of course, you have to fill in the private key (the public key is included in the script for reference only. You also need to fill in the gitorious URL.

在post-receive钩子中,您必须放置:

In the post-receive hook, you have to put:

path/to/push-gitorious.sh run

运行opti on是很重要的,否则它会直接运行ssh。

The run option is important, otherwise it will run ssh directly.

警告:不对远程主机身份进行检查。您可以从ssh命令行中删除该选项,并根据需要自定义 known_hosts 。在这个用例中,我不认为这很重要。

Warning: no checking is done on the remote host identity. You can remove the option from the ssh command line and customize known_hosts if you want to. In this use case, I don't think it's important.

这篇关于如何指定在git中使用哪个SSH密钥进行git push以获得作为镜像的gitorious?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆