[RAILS] Procedure for uploading from Cloud9 to GitHub repository (including error resolution method)

-** I have run Git with Git Bash etc. ** ** -** GitHub repository has been created. ** **

By the way

-** "Because I started development on AWS Cloud9 for the first time, I want to upload the created source to the existing GitHub repository" **

I made it for those who say.

I myself used Git / GitHub with git bash, but when I used git with Cloud9 for the first time, I got various errors and I was in trouble, so I thought I wanted an article for Cloud9 and created it. ..

The GitHub repository is assumed to be "public". (The procedure seems to be different if it is private)

Introduction

First, prepare a cloud9 terminal. Just in case, make sure you have git. (Of course it is included in Cloud9)

$ git --version
git version 2.14.5

Create local repository

Go to any directory where you want to create a local repository

$ git init

Will generate a ".git" directory (local repository).

Add to staging area

First, add to the staging area

$ git add [file]

Commit to local repository

Then commit the files in the staging area to your local repository

$ git commit -m "[Any message]"

Remote repository registration and git push

If you can commit, registration is complete in the local repository of git created on cloud9. Next, you need to push (upload) to the GitHub repository (remote repository). First of all, from the registration of the remote repository

$ git remote add origin [email protected]:[Own GitHub repository].git

afterwards,

$ git push -u origin master

I think there are people who get the following error here.

Permission denied (publickey). 
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists. 

The above error has the following meaning for each line:

Privilege denied with public key.
The remote repository cannot be read.
Do you have access rights? Or does the repository itself exist?

Let's solve the error.

I think that the registration of the remote repository has been completed earlier. Therefore, the error message "Does the remote repository itself exist?" Is not a problem.

Now let's get access.

** You need a public key to access GitHub. ** ** You can access it by creating a public key in the development environment and registering the public key on GitHub.

Creating a public key

First, create a public key in a directory inside cloud9.

$ ssh-keygen -t rsa -C "[Your email address where you registered the remote repository(・ ・ ・@gmail.com etc.)]"

(It can be executed without the comment part after -C, but it seems that it is common to specify the email address registered on GitHub)

"Ssh-keygen" is a command to create an SSH (Secure SHell) public key and private key. By the way, the meaning of the option is

--"-Trsa" ・ ・ ・ Specify the encryption format of the key to be created with "rsa" -"-C" Comment "" ・ ・ ・ Specify a comment

If you want to know more details, I think the following article is good. https://www.atmarkit.co.jp/ait/articles/1908/02/news015.html

When executed, the following message will appear. There are 3 lines starting with Enter ~, and you will be asked to enter each line, but there is no problem if you press Enter without entering anything.

Generating public/private rsa key pair.
Enter file in which to save the key (/home/ec2-user/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again:

If you press Enter three times above, the following message will be displayed and the public key will be created. (The part of randomart image is rewritten appropriately)

Your identification has been saved in /home/ec2-user/.ssh/id_rsa.
Your public key has been saved in /home/ec2-user/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:……………………………………@gmail.com
The key's randomart image is:
+---[RSA 2048]----+
|....................       |
| ..........        |
|   ......= ......       |
|    *..... .....        |
|   ...... BS+       |
|    =...........o..      |
ho
host github
|   .................       |
|    ......o*=.....+       |
|    .E..........*......      |
+----[SHA256]-----+

Then check the .ssh directory and you'll see that the public key has been created.

$ ls ~/.ssh/
authorized_keys  id_rsa  id_rsa.pub  known_hosts

Copy the contents of the id_rsa.pub file

Display the file contents of id_rsa.pub with the following command and copy the contents. (It is OK to copy all the contents of the file instead of looking at the contents with the less command)

$ less ~/.ssh/id_rsa.pub

Copy the part of the file that starts with ssh-rsa below. (It doesn't matter whether you include the email address in the copy or not because it doesn't change.)

ssh-rsa ...
・ ・ ・ ・ ・ ・ ・ ・ ・ ・ ・ ・ ・ ・ ・ ・[mail address]

Register your public key on GitHub

I will register the part I just copied to GitHub, but first access GitHub from a browser and click "settings" from my profile image on the upper right

image2.png

Then select "SSH and GPG keys" from the menu on the left and click "New SSH keys".

image3.png

Then, the registration screen will appear. Paste the wording starting with "ssh-rsa" that you copied earlier into the text area of the "key" part of the registration screen. By the way, the title doesn't matter.

image4.png

After pasting, click "Add SSH key".

Here, there are many articles that say that you can succeed in connecting by typing the following command, but there are cases where an error occurs and the connection fails.

$ ssh -T [email protected]

Creating a config file

If the connection fails, create a file called "config" in the .ssh directory. Create a file and describe its contents with vim.

$ vim ~/.ssh/config

Paste the following wording into the contents of the config file. Please change the path after ** IdentityFile ** to the path that stores your own "id_rsa" file.

Host github github.com
  HostName github.com  IdentityFile ~/.ssh/id_rsa
  User git

Permission settings for newly created files

After creating it, set the authority. Now that you've created a new file, set the appropriate permissions so that it can run properly.

Initially, the config file has no permissions.

But this time, we need to put it in a state where "** owner ** has ** read permission ** and ** other users ** do not have ** permission **".

Therefore, the authority should be 600 or 400. (Because you may or may not have write permission)

Do not give authority to anyone other than the owner. For 600, do the following:

$ chmod 600 ~/.ssh/config

ssh execution

Then do the following:

$ ssh -T [email protected]

Execute it, and if the following character string is output, it's OK!

Hi [username]! You've successfully authenticated, but GitHub does not provide shell access.

** Now you can git push. ** **

Finally, do a git push

You can upload files from your local repository to a remote repository with git push.

$ git push origin master

That's it!

Recommended Posts

Procedure for uploading from Cloud9 to GitHub repository (including error resolution method)
How to clone github remote repository from atom
[Python] Local → Procedure for uploading files to S3 (boto3)
Newton's method for machine learning (from one variable to multiple variables)