Sunday, February 9, 2014

Installing and using Git on Ubuntu 12.04 from command line

Git is a version control system which can handle any sized project with speed and efficiency; I personally use it for all of my projects, most of which are private repository hosted with bitbucket. If you are unfamiliar with this type of version control system, go to git-scm and read all about Git.

Project: Installing Git from command line (Ubuntu 12.04 LTS)
Difficulty: 1/10
Prior knowledge required: none - understanding of apt repositories suggested but not required.
Time: 15-30 minutes

If you have continuted reading this, I assume you're more interested on how to install Git on Ubuntu 12.04 (other versions of Ubuntu should be very close, but this exact method has only been tested on 12.04 LTS).

Getting Started


Before installing Git itself, you should update all of your apt repositories and make sure there are no errors updating, and if there are go through them and correct any issues you may have.

 user@xynteq~$ apt-get update

Once you have done this, use apt-get to install git and answer yes when it asks you if you would like to continue.

user@xynteq~$ sudo apt-get install git

From here, you can either use the default name and email for the account on your system, or you can specify specifically who you are and what your email address is with the following two commands:

user@xynteq~$ git config --global user.name "YOUR NAME"
user@xynteq~$ git config --global user.email "your.email@yourdomain.com"

This will let git know exactly who is pushing or pulling from the repositories. Before you can actually begin pushing or pulling, you need to authenticate yourself with the git servers. I will be using Bitbucket as an example, however most (if not all) major repository systems should be very similar.

The first thing that needs to be done is create a ssh key which is done from the terminal from your server. For this, we will be using the ssh-keygen command, with the type set to rsa, and with a comment (you email) to distinguish it from other keys. I suggest using a strong pass phrase here, because if your key gets hijacked you could be in trouble.

user@xynteq~$ ssh-keygen -t rsa -C "your.email@yourdomain.com"

NOTE: You cannot recover these passwords, if you forget it you need to create an entirely new key.


Once you have generated your public and private keys, nagivate to your ~/.ssh folder and view your public key (ends in .pub). Your key will begin with the key type, ssh-rsa, the actual key, and the comment at the end; all of this is needed later when we upload our key to bitbucket.

Click settings > Manage Account
 Once you are logged into your Bitbucket account, navigate to your Manage Accounts pane, select SSH keys, then add a key. The dialog that opens gives you a place to name the key, and a space for the key its self. Copy the ENIRE TEXT from the key we looked at earlier (including the comment) into this box, name it (I suggest using a computer name that you will associate it with incase you need to revoke keys in the future) and click Add Key.

You now have Bitbucket set up for you to pull from repositories!



Now that you are all setup to pull from a repository lets make your first pull! I have a dummy repository setup on Bitbucket, it includes nothing but a single text file (hello.txt), so lets pull from that as it wont take any  (well, negligible amount of) extra system resources.

First, create yourself a directory for repositories to go into, mine will be a folder named repos in the home directory.

user@xynteq~$ mkdir repos

Now that we have a folder to pull the project into, lets execute the actual pull command.

user@xynteq~$ git clone git@bitbucket.org:svoss/git-test-repo.git
Cloning into 'git-test-repo'...
Enter passphrase for key '/home/user/.ssh/id_rsa': 
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.

If you saw the message in italics above, congratulations! You pulled you first git! Try to navigate into the folder git-test-repo and read the hello.txt inside!

No comments:

Post a Comment