Create a repository in Github from a remote Server via SSH

3 minute read

GitHub is a code hosting platform for version control and collaboration. It lets you and others work together on projects from anywhere.

How to create a repository in Github from a remote Server with SSH?

A repository is usually used to organize a single project. Repositories can contain folders and files, images, videos, spreadsheets, and data sets – anything your project needs.

github

Your first time with git and github

If you’ve never used git or github before, and you want use them with your private server, there are a bunch of things that you need to do. It’s very well explained on github, but repeated here for completeness.

  • Get a github account.

  • Download and install git. ( we assume that your server it has git installed)

  • Set up git with your user name and email.

    • Open a terminal/shell in your remote server and type:

      git config --global user.name "Your name here"
      git config --global user.email "[email protected]"
      

      (Kep the " "; during your setup of username and email.)

      I also do:

      git config --global color.ui true
      git config --global core.editor nano
      

      The first of these will enable colored output in the terminal; the second tells git that you want to use nano.

  • Set up ssh on your computer. I like Roger Peng’s guide to setting up password-less logins. Also see github’s guide to generating SSH keys.

    • Look to see if you have files ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub.

    • If not, create such public/private keys: Open a terminal/shell and type:

      ssh-keygen -t rsa -C "[email protected]"
      
    • Copy your public key (the contents of the newly-created id_rsa.pub file)

    • You can download the file .pub copy and delete

      On a Windows, in the terminal/shell, type:

      scp [email protected]:~/.ssh/id_rsa.pub C:\Users\username\Documents
      

      In case you have Linux /Mac you can use

      scp [email protected]:~/.ssh/id_rsa.pub  /Users/username/Documents/
      
  • You open your any editor in your Documents folder, copy the string and then go to the Github page.

  • Paste your ssh public key into your github account settings.

    • Go to your github Account Settings

    • Click “SSH Keys” on the left.

    • Click “Add SSH Key” on the right.

    • Add a label (like “My laptop”) and paste the public key into the big text box.

    • In a terminal/shell, type the following to test it:

    • If it says something like the following, it worked:

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

Don’t forget delete your .pub file in your documents folder of your local computer.

del C:\Users\username\Documents\id_rsa.pub 

Security Issues

If you got the error

`@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0704 for ~/.ssh/id_rsa

That means that you have to change the permissions of your private key

Keys need to be only readable by you:

chmod 400 ~/.ssh/id_rsa

If Keys need to be read-writable by you:

chmod 600 ~/.ssh/id_rsa

600 appears to be fine as well (in fact better in most cases, because you don’t need to change file permissions later to edit it).

The relevant portion from the manpage (man ssh)

Create new repo from local to Github

First we need to create a repository in the website of Github

  1. In the upper-right corner of any page, use the drop-down menu, and select New repository.
  2. Type a short, memorable name for your repository. …
  3. Optionally, add a description of your repository. …
  4. Choose a repository visibility. …
  5. Click Create repository.

Lets assume that your repository is called newrepo and you have a project in your server.

Lets add this folder of your project to the Github

  1. Go to your terminal and enter to your remote server via ssh

  2. Find the folder that you want to add to your Github, lets says is named myproject,

    cd myproject
    
  3. Write down the following commands:

echo "# newrepo" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote set-url origin [email protected]:username/newrepo.git
git push -u origin main

ad the end you got

Enter passphrase for key '/home/username/.ssh/id_rsa':
Counting objects: 52, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (50/50), done.
Writing objects: 100% (52/52), 247.77 KiB, done.
Total 52 (delta 12), reused 0 (delta 0)
remote: Resolving deltas: 100% (12/12), done.
To [email protected]:username/newrepo.git
 * [new branch]      main -> main
Branch main set up to track remote branch main from origin.

if you go to your website of Github you will see you new published repo updated.

Check setup

If you are interested to check if your setup is correct type

git config --list

and you got something like:

user.name=username
[email protected]
color.ui=true
core.editor=nano
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
[email protected]:username/newrepo.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.main.remote=origin
branch.main.merge=refs/heads/main

Congratulation we have created a repository in Github via SSH from your remote server.

Posted:

Leave a comment