Git

Pulling a git repo from github to your Ubuntu server

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 [email protected]

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 "[email protected]" # 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 [email protected]

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 [email protected]

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 [email protected]: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.

Published by Jonathan Whiting

I enjoy sharing what I am learning and hopefully it's of interest and help to you. I live in Canada with my wife. Follow me on Twitter.

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

  1. Keith says:

    By far and away one of the best articles for this i have ever read. Followed it step by step and Boom! sorted 🙂 Thanks

    1. Jonathan Whiting says:

      Thanks Keith

  2. Sharif says:

    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. Jonathan Whiting says:

      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.

  3. JJ says:

    Thanks a million!!!!!!! It helped me a lot. Keep up the good work, sir 🙂

    1. Jonathan Whiting says:

      No problem JJ

  4. Gabriel Scavassa says:

    Thanks man!! Works fine to me!

    1. Jonathan Whiting says:

      Hi Gabriel, glad it helped you out.

  5. Sergei says:

    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. Jonathan Whiting says:

      Hi Sergei,

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

  6. Typo in “Copy the contents of the file id_rsa.pup”

    Should read as:

    “Copy the contents of the file id_rsa.pub”

    1. Jonathan Whiting says:

      Good catch, it’s updated. Cheers

  7. A very easy to follow set of instructions to clone github repositories. An excellent resource.

    1. Jonathan Whiting says:

      Thanks Sam

  8. Ilian says:

    You really need to add the instruction for ssh-add, as suggested by ptv over a month ago

  9. ptv says:

    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).

    1. Jonathan Whiting says:

      Thanks and good suggestion, will do.

    1. Jonathan Whiting says:

      Hi Ernest, good tip.

  10. Uday says:

    Very useful … I’m a rookie Ubuntu user and still was able to get the code easily. Thanks!

  11. Margaret M says:

    Thank you, this was super helpful.

    1. Jonathan Whiting says:

      Thanks Margaret

  12. leotan says:

    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. Jonathan Whiting says:

      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.

  13. Vijay says:

    Very very helpful write-up , got it right the first time thanks to your pointers.

  14. 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. Jonathan Whiting says:

      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 *