Pulling a git repo from github to your Ubuntu server

A quick tutorial showing you how to install and setup git on an Ubuntu server and pull a repo from Github.

What green means: any lines that you need to replace in this tutorial will be in green.


How to check if Git is installed on your Ubuntu server (or your current version):

git --version

If you get -bash: git: command not found, then git is not installed

How to install Git on Ubuntu:

It’s very easy using apt-get. The program installs everything you need in one command.

sudo apt-get install git

You will be asked if you want to continue. Press ‘y’

After it finishes downloading it is ready to go.

How to setup Git:

After Git is installed you will need to enter your username and email

git config --global user.name "Your Name"
git config --global user.email youremail@example.com

How to Generate an SSH key:

1. First check to see if there exists and .ssh directory

cd ~/.ssh

If there is no directory you will see: “No such file or directory”

2. If the directory exists and a key exists backup and remove SSH Key

ls# Lists all the subdirectories in the current directory
# config  id_rsa  id_rsa.pub  known_hosts

mkdir key_backup# Makes a subdirectory called "key_backup" in the current directory

cp id_rsa* key_backup# Copies the id_rsa keypair into key_backup

rm id_rsa*# Deletes the id_rsa keypair

3. Generate a new SSH Key

# Creates a new ssh key using the provided email

ssh-keygen -t rsa -C "your_email@youremail.com" # Generating public/private rsa key pair. # Enter file in which to save the key (/home/you/.ssh/id_rsa):

Next enter a passphrase (or leave it blank) – You will use your passphrase every time you do a Git pull or push. If you leave it blank then it won’t ask you for one.

Now you should see something like this.

Your identification has been saved in /home/you/.ssh/id_rsa.
# Your public key has been saved in /home/you/.ssh/id_rsa.pub.
# The key fingerprint is:
# 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@youremail.com

How to add your SSH Key to Github:

Copy the contents of the file id_rsa.pub

It’s important to copy the full key with no extra spaces or missing characters.

Next go to your Github account settings

Click on the “SSH KEYS” menu item on the left

Then click “Add SSH key”

Paste the key into the key field and click “Add Key”

You will be asked for you password. Enter it.

How to test if your SSH key is added:

ssh -T git@github.com

You will see the following warning

The authenticity of host 'github.com (207.97.227.239)' can't be established.
# RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
# Are you sure you want to continue connecting (yes/no)?

Type “yes”

If SSH key was properly added to Github you will see

Hi Username! You’ve successfully authenticated, but GitHub does not provide shell access.

How to clone your Github repo into your Ubuntu server: 

cd into the directory that you want to clone your repo

Next go to your respository hosted on Github

You will see a text field that has the SSH git clone info. Copy that.

Now in your terminal type git clone and then paste the above text field after (as seen below)

git clone git@github.com:Username/gitrepo.git

You should now see the git repo in your server

If you get an error like this “git fatal: could not create work tree dir” this means your Ubuntu user does not have permission to create files in the current directory. There is an easy way to give your user permission to edit and create files in a directory owned by another user.

I help people build better web apps and make a difference in the world. I enjoy sharing what I am learning and find interesting. I live on the Left Coast of Canada with my wife. Follow me on Twitter.

27 Replies to “Pulling a git repo from github to your Ubuntu server”

  1. Jonathan,

    An excellent article. Like others I got it going (almost) first time. I say almost because I had two-factor authentication on my GitHub login and that failed my command line login. Once I realised this and disabled TFA it worked great.

    Any workaround to keep TFA would be appreciated.

    Thanks.

    1. Hi Sharif,

      Thanks for the compliment and the heads-up about the 2-step authentication. When I have some time will look into some solutions to keep TFA enabled.

  2. Thank you for the Article! It’s great

    But for was me need this operation

    Add your generated SSH key to the ssh-agent:
    ssh-add ~/.ssh/id_rsa

    1. Hi Sergei,

      Thanks for commenting. There are a few responses about ssh add. This will help others. thanks

  3. This was very easy to follow, super helpful! Thank you!

    One comment:
    Please add a section on adding the SSH key to the SSH agent in Ubuntu (ssh-add).

  4. hi, thank you for your tutorial. im getting issues when i have a passphrase but will my server be still secure if i dont have a passphrase? and if not, how do i make git pull work when having a passphrase? thanks

    1. Hi Leotan,

      Your server will still be secure without a git passphrase set. If you have multiple server users and they have access to your git repos, then having a passphrase may be useful if you don’t want to give them ability to pull or push to your git repo. But generally passphrases are unnecessary.

  5. Not sure when this was written, but it was EXTREMELY helpful – thank you. I’ve been struggling getting git working cleanly without having to ask me for credentials every. single. time. – this did the trick.

    Do you have any similarly-helpful articles on basic git usage now that this article has laid a good starting foundation?

    1. Hi John, thanks for your feedback. I don’t have any other articles yet, but I could definitely put some together. What would you be most interested in learning?

Leave a Reply

Your email address will not be published. Required fields are marked *