如何在 EC2 上推送到 git
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4632749/
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
How to push to git on EC2
提问by zengr
I am trying to follow thisinstruction. I have a local git repo and when I do a git push, I need the repo to be pushed to my EC2 instance.
我正在尝试遵循此说明。我有一个本地 git 存储库,当我执行 git push 时,我需要将存储库推送到我的 EC2 实例。
But, in the above tutorial, when I do a git push origin master
, I get Permission denied (publickey)
error because I did not specify the identity file.
但是,在上面的教程中,当我执行 a 时git push origin master
,我收到Permission denied (publickey)
错误,因为我没有指定身份文件。
Say, I login to EC2 like this: ssh -i my_key.pem [email protected]
说,我像这样登录到 EC2: ssh -i my_key.pem [email protected]
So, can I do something similar here to: git -i my_key.pem push origin master
or set the identity file in .git/config
那么,我可以在这里做类似的事情吗:git -i my_key.pem push origin master
或将身份文件设置在.git/config
So, how can I set it up?
那么,我该如何设置呢?
Update: Output of git config -l
更新:输出 git config -l
user.name=my name
[email protected]
github.user=userid
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
remote.origin.url=ec2_id@my_e2_ip_address:express_app
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
Update (from @Jon's comment):
更新(来自@Jon 的评论):
If you have your key in an odd path just run ssh-add /private/key/path
. This worked for me.
如果您的密钥位于奇怪的路径中,请运行ssh-add /private/key/path
. 这对我有用。
采纳答案by realgt
To copy your local ssh key to amazon try this
要将您的本地 ssh 密钥复制到亚马逊,请尝试此操作
cat ~/.ssh/id_?sa.pub | ssh -i amazon-generated-key.pem ec2-user@amazon-instance-public-dns "cat >> .ssh/authorized_keys"
replacing the names of the key and amazon ec2 public dns, of course.
当然,替换密钥和亚马逊 ec2 公共 dns 的名称。
you will then be able to setup your remote on amazon
然后你就可以在亚马逊上设置你的遥控器了
回答by TinyTimZamboni
The instructions listed herewere more useful to me.
此处列出的说明对我更有用。
From the link:
从链接:
Adjust your ~/.ssh/config
and add:
调整您的~/.ssh/config
并添加:
Host example
Hostname example.com
User myuser
IdentityFile ~/.ssh/other_id_rsa
Now use the ssh host alias as your repository:
现在使用 ssh 主机别名作为您的存储库:
$ git remote add origin example:repository.git
$ git pull origin master
And it should use the other_id_rsa
key!
它应该使用other_id_rsa
密钥!
回答by devdrc
On your local machine, edit your ~/.ssh/config and add:
在您的本地机器上,编辑您的 ~/.ssh/config 并添加:
Host example
Hostname example.com
User myuser
IdentityFile ~/.ssh/YOURPRIVATEKEY
You should be able to login to your instance with "ssh example". Remember your private key should be chmod 400. Once you can ssh in without using "ssh -i mykey.pem username@host", do the following.
您应该能够使用“ssh 示例”登录到您的实例。请记住,您的私钥应该是 chmod 400。一旦您可以在不使用“ssh -i mykey.pem username@host”的情况下通过 ssh 进入,请执行以下操作。
On your EC2 instance, initialize a bare repository, which is used to push to exclusively. The convention is to add the extention ".git" to the folder name. This may appear different than your local repo that normally has as .git folder inside of your "project" folder. Bare repositories (by definition) don't have a working tree attached to them, so you can't easily add files to them as you would in a normal non-bare repository. This is just they way it is done. On your ec2 instance:
在您的 EC2 实例上,初始化一个裸存储库,用于独占推送。约定是将扩展名“.git”添加到文件夹名称。这可能与通常在“项目”文件夹内具有 .git 文件夹的本地存储库不同。裸存储库(根据定义)没有附加到它们的工作树,因此您不能像在普通的非裸存储库中那样轻松地向其中添加文件。这只是他们完成的方式。在您的 ec2 实例上:
mkdir project_folder.git
cd project_folder.git
git init --bare
Now, back on your local machine, use the ssh host alias when setting up your remote.
现在,回到您的本地机器上,在设置远程时使用 ssh 主机别名。
git remote add ec2 EXAMPLEHOSTFROMSSHCONFIG:/path/to/project_folder.git
Now, you should be able to do:
现在,您应该能够执行以下操作:
git push ec2 master
Now your code is being pushed to the server with no problems. But the problem at this point, is that your www folder on the ec2 instance does not contain the actual "working files" your web-server needs to execute. So, you need to setup a "hook" script that will execute when you push to ec2. This script will populate the appropriate folder on your ec2 instance with your actual project files.
现在您的代码被毫无问题地推送到服务器。但此时的问题是,您在 ec2 实例上的 www 文件夹不包含您的网络服务器需要执行的实际“工作文件”。因此,您需要设置一个“挂钩”脚本,该脚本将在您推送到 ec2 时执行。此脚本将使用您的实际项目文件填充 ec2 实例上的相应文件夹。
So, on your ec2 instance, go into your project_folder.git/hooks directory. Then create a file called "post-receive" and chmod 775 it (it must be executable). Then insert this bash script:
因此,在您的 ec2 实例上,进入您的 project_folder.git/hooks 目录。然后创建一个名为“post-receive”的文件并 chmod 775 它(它必须是可执行的)。然后插入这个 bash 脚本:
#!/bin/bash
while read oldrev newrev ref
do
branch=`echo $ref | cut -d/ -f3`
if [ "ec2" == "$branch" -o "master" == "$branch" ]; then
git --work-tree=/var/www/example.com/public_html/ checkout -f $branch
echo 'Changes pushed to Amazon EC2 PROD.'
fi
done
Now, on your local machine, do a "git push ec2 master" and it should push the code to your bare repo, and then the post-receive hook script will checkout your files into the appropriate folder that your webserver is configured to read.
现在,在您的本地机器上,执行“git push ec2 master”,它应该将代码推送到您的裸仓库,然后接收后挂钩脚本会将您的文件检出到您的网络服务器配置为读取的适当文件夹中。
回答by Jon
You need to generate and upload a SSH key onto the EC2 instance. Follow this tutorial: http://alestic.com/2010/10/ec2-ssh-keys
您需要生成 SSH 密钥并将其上传到 EC2 实例。按照本教程:http: //alestic.com/2010/10/ec2-ssh-keys
回答by Alastair
- Run
ssh-keygen
locally - In your local
~/.ssh/
directory you should now see a public keyfile calledid_rsa.pub
- copy the contens of this file to the/etc/ssh/authorized_keys
file, which is located on your remote server.
- 在本地运行
ssh-keygen
- 在您的本地
~/.ssh/
目录中,您现在应该看到一个名为- 将此文件的内容复制到位于远程服务器上的文件的公钥文件。id_rsa.pub
/etc/ssh/authorized_keys
You can either copy and paste the contents, or upload the file to your remote server first and use the following command:
您可以复制并粘贴内容,也可以先将文件上传到远程服务器并使用以下命令:
cat id_rsa.pub >> /etc/ssh/authorized_keys
cat id_rsa.pub >> /etc/ssh/authorized_keys
回答by rikkitikkitumbo
I found this was the quickest way: https://gist.github.com/matthewoden/b29353e266c554e04be8ea2058bcc2a0
我发现这是最快的方法:https: //gist.github.com/matthewoden/b29353e266c554e04be8ea2058bcc2a0
Basically:
基本上:
ssh-add /path/to/keypair.pem
(the"-add" needs to be RIGHT AFTER the ssh)
ssh-add /path/to/keypair.pem
(“-add”需要紧跟在 ssh 之后)
check to see if it worked by: ssh ubuntu@crazylongAWSIP
(maybe your username is not ubuntu)
检查它是否有效:( ssh ubuntu@crazylongAWSIP
也许您的用户名不是 ubuntu)
After that you can set up a git repo on your ec2 and push to it:
之后,您可以在 ec2 上设置一个 git repo 并推送到它:
git remote add origin [email protected]:/path/to/your/repo-name.git
git config --global remote.origin.receivepack "git receive-pack" # needed for aws ec2 stuff.
git push origin master
Your options are to set up a 'bare' git repo on your ec2 (which means other git repos can pull from it and push to it, but it won't hold any files), or you can set up a NORMAL repo and push to it directly (my preference if you want to push local changes to your ec2 without having to constantly ssh into your ec2).
您的选择是在您的 ec2 上设置一个“裸”的 git 存储库(这意味着其他 git 存储库可以从中拉取并推送到它,但它不会保存任何文件),或者您可以设置一个 NORMAL 存储库并推送直接访问它(如果您想将本地更改推送到您的 ec2,而不必经常 ssh 进入您的 ec2,我的偏好)。
If you want to set up a NORMAL repo on the ec2, ssh in to the ec2, do a git init
where you want, and then do this:
如果您想在 ec2 上设置一个 NORMAL 存储库,请通过 ssh 连接到 ec2,git init
在您想要的位置执行以下操作,然后执行以下操作:
git config receive.denyCurrentBranch updateInstead
See: cannot push into git repositoryfor explanation of "recieve deny current branch"
请参阅:无法推送到 git 存储库以了解“接收拒绝当前分支”的解释
回答by guero64
I'm not posting anything new here, I think, but I had to dig through the above answers to address my particular case. I have an Ubuntu instance on EC2.
我想我不会在这里发布任何新内容,但我不得不深入研究上述答案以解决我的特殊情况。我在 EC2 上有一个 Ubuntu 实例。
To login to my instance, I needed to do:
要登录我的实例,我需要执行以下操作:
ssh -i "pemfile.pem" ubuntu@very-long-amazon-address
the key file "pemfile.pem" had to be in quotes.
密钥文件“pemfile.pem”必须用引号引起来。
I added the remote:
我添加了遥控器:
remote add origin ubuntu@very-long-amazon-address/home/ubuntu/git/REPO/gitfile.git
But when I tried to push:
但是当我试图推动时:
git push origin master
I got:
我有:
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
To fix, I did:
为了修复,我做了:
/<path to pemfile>/pemfile.pem
Which gave me a response,
这给了我一个回应,
Identity added: /<path to pemfile>/pemfile.pem (/<path to pemfile>/pemfile.pem )
After which the push went through fine.
之后推动顺利。
回答by Danny
I was getting permission deniedwhen deploying via source control and couldn't figure out why. I realized my user I was creating an ssh key for (named ubuntu, also the recommended login for my ec2 server) was not the user who was responsible for cap deploy (root). Running an ssh-keygen for root and uploading that ssh key as a deploy key to bitbucket solved my issues.
通过源代码管理进行部署时,我的权限被拒绝,但不知道为什么。我意识到我正在为其创建 ssh 密钥的用户(名为 ubuntu,也是我的 ec2 服务器的推荐登录名)不是负责 cap deploy 的用户(root)。为 root 运行 ssh-keygen 并将该 ssh 密钥作为部署密钥上传到 bitbucket 解决了我的问题。
回答by JoenasE
I know I'm too late for this but I just wanted to share this article which in just seconds I've successfully pushed to EC2 git repo
我知道我为时已晚,但我只是想分享这篇文章,在短短几秒钟内我已成功推送到 EC2 git repo
http://shirtdev.wordpress.com/2011/05/04/setting-up-a-git-repository-on-an-amazon-ec2-instance/
http://shirtdev.wordpress.com/2011/05/04/setting-up-a-git-repository-on-an-amazon-ec2-instance/
回答by rckehoe
Here is the EASIEST way that worked great for me... I was having trouble cloning a repository... it was not recognizing the SSH Key I created... Instead of changing your config file and all that, I simply copied the REAL ssh key it was trying to connect with and I added this to bitbucket... here is the command:
这是对我有用的最简单的方法......我在克隆存储库时遇到了麻烦......它无法识别我创建的SSH密钥......而不是更改你的配置文件和所有这些,我只是复制了REAL它试图连接的 ssh 密钥,我将它添加到 bitbucket ......这是命令:
sudo vi /root/.ssh/id_rsa.pub
Used VI to open the REAL RSA key and copied the content and pasted into bitbucket... Done!
使用VI打开REAL RSA密钥并复制内容并粘贴到bitbucket中......完成!