Git Started

Installing Git on VirtualBox with Ubuntu 14.04


For those just tuning in, this is the latest in a series of posts that document my journey from industrial engineer to developer while at the same time becoming a new mom (2.5 weeks to go!). Last post was on my fun with installing an Ubuntu Virtual Machine on my Windows 7 host. It was a doozy. Fortunately, getting Git up and running was much less of a headache. I knew nothing about Git or GitHub when I first started learning programming, so this is a bit of what I’ve picked up in the last couple of weeks. It’s also the process I used to get everything installed, so I hope it will be helpful to anyone else just getting started!

What is Git?

This is a question I had not so long ago, so for those not familiar with it yet, I think an introduction is due. Git is a version control system. Like every other VCS, it allows developers to track changes they’re making to code, revert back to previous versions if needed, and ‘branch’ out from their main code to create alternate versions of their program/site/app that can be added back into the main code later as new features or bug fixes. Git is specifically a distributed VCS, which means every member of a team working together on a project has their own copy of the entire project, rather than “checking out” a central file that everyone shares. Git is one of the most popular VCS options and can sync up to its online repository, GitHub, which we’ll talk a bit about later on.

Installing Git

Now that we know more about what Git is, there are two ways to install it if you’re using an Ubuntu box like the one I set up in my last post.

The first way is a binary installation, which will install a Git package that came with the Ubuntu version you downloaded. This is the easier way, but not what I chose to do because it’s less flexible. Nevertheless, in case you’d rather just do it quick, here’s the terminal code for the binary Git installer:

sudo apt-get update && sudo apt-get install git

That’s it. Told you it was easy.

Source installation is a little more involved, but the reason I chose to do it this way is you can make sure that you download and install the latest version of Git rather than whatever came with your OS installation.

The first step is to install the packages that Git is dependent on. These all have binary installers included with the system, so it’s pretty quick:

sudo apt-get update
sudo apt-get install build-essential libssl-dev libcurl4-gnutls-dev libexpat1-dev gettext unzip

Once that update is done, it’s time to grab the link to the latest stable version of Git. You do this by heading over to the Git project page on GitHub. When you get there, you should see a drop-down menu that says branch:master on it. Open the menu and click on the ‘tags’ tab. When I was installing Git, v2.0.4 was the latest stable release… but that will change with time, so just select the very first option you see that doesn’t have ‘rc’ at the end of the tag name. This will make sure that you’re getting the latest stable release of Git and not a work in progress.

Once you select that, look for a Download ZIP button on the left side of the page. Right-click that and copy the link address. Don’t actually download the zip, Ubuntu will pull it for you in the next step.

Back in your terminal window, type wget and then paste the address you copied. The code will vary depending on what the last stable release is at the time you’re installing, but here’s mine:

wget https://github.com/git/git/archive/v2.0.4.zip -O git.zip

Then unzip the file and change directories so you’re in the new folder:

unzip git.zip
cd git-*

Next you ‘make’ the installation package and then install it:

make prefix=/usr/local all
sudo make prefix=/usr/local install

That’s it! Git is installed and ready to use on your machine. However, there are a few other things you might want to do now while your head is already into “Git installing” mode.

Connecting to GitHub

Like I mentioned earlier, GitHub is the online repository system for Git. You can use Git without ever opening a GitHub account, but there are some major advantages to GitHub that make it worth working with. First, it’s an amazing way to find and contribute to open-source projects. You can “fork” a repository someone else owns and either make changes to it for your own unique twist on the project, or make changes that you send back to the original owner in a “pull request” that the owner can then “merge” back into the main project.

For every project you’re involved with, you get attribution and a link on your profile. This also makes GitHub a sort of resume for programmers, where developers can show off their skills and projects to each other and potential employers. There’s also a social aspect, where wikis can be generated around a project and the public can file bug/issue reports, encouraging further collaboration and learning.

Convinced? Great. I’m not going to go through opening your own GitHub account here, just how to connect your local Git install to an account you’ve already opened. If you need to do the first step, just head over to http://www.github.com and follow the directions to open an account before continuing below.

Tell Git Who You Are

Once you have a GitHub account, go back to your machine’s terminal and tell Git who you are. It will use this information to sign your code commits and to sync with your GitHub account. Replace YOUR NAME with your actual name (not a username), for example mine is “Cathleen Berry”. Your email needs to match the email you signed up for a GitHub account with.

git config --global user.name "YOUR NAME"
git config --global user.email "YOUR EMAIL ADDRESS"

Authenticate to GitHub

The next step is to prove to GitHub that you are who you say you are ☺ There are two ways to do this, either through HTTPS or through SSH. I’m going to go over the SSH method because it’s the recommended and more secure method.

Step 1: Check for existing SSH keys

Type this in your terminal to check if there are already SSH keys generated on your machine.

ls -al ~/.ssh

If you find a file in the listing that gets returned that’s named either “id_rsa.pub” or “id_dsa.pub” then you can skip to step 3.

Step 2: Create an SSH key

If you didn’t have either of the files mentioned above (which you probably won’t if this is your first time setting up something like GitHub), then enter the command below in the terminal, with your email address:

ssh-keygen -t rsa -C "your_email@example.com"

It will ask you to “Enter a file in which to save the key”, you can just press Enter to keep going. Then it will ask you to enter a passcode and confirm it. Finally it will show you a “fingerprint” unique to your email/machine combo. The last step is to add your new fingerprint key to what’s called the ssh-agent. Use these commands to do that:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa

Step 3: Add your SSH key to GitHub

Now you need to copy the contents of the SSH key file. You can do this by either finding the ~/.ssh/id_rsa.pub file, opening it in a text editor, and copying the contents of the file manually, or you can type the following in the terminal to install a file copier and copy the key that way:

sudo apt-get install xclip
xclip -sel clip < ~/.ssh/id_rsa.pub

The go to the SSH Keys page of your GitHub admin settings.

Click Add SSH key on the top left side of the page. Then in the Title field, add a descriptive label for the new key. For example, I called mine “GitHub for Linux VB”. Then just paste your key into the “Key” field. It will look something like the jumbled mess shown here. Then click the “Add Key” button and confirm the entry by typing in your GitHub password. Once you’re done with this step, head back to the terminal.

Step 4: Make Sure It’s Working

In the terminal, run a few more commands to finalize everything. First try to connect to GitHub now that you’re authenticated:

ssh -T git@github.com

That may return the following warning. This isn’t a problem! As long as the fingerprint it shows you matches the one below, go ahead and type ‘yes’ and hit enter.

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

After you type yes, you should get the following message:

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

Shell access isn’t something you need anyways, so we’re finally all done connecting our machine’s Git to our GitHub account!

Create a global .gitignore

This next part is by no means a necessary part of the setup, but rather an answer to an annoying problem I ran into pretty early on while using Git on my Linux box. Every time I opened a file in a text editor, a local backup file was created in my working directory. So if I edited index.html, a file named “index.html~” was placed right next to it. These files are hidden from the normal file explorer in Linux, but Git picks them up as added files, which was causing a really ditry and crowded working directory. One solution to this problem is to tell Git to always ignore files with a ‘~’ at the end.

You do this by creating a global .gitignore file, which is a list of rules for ignoring certain files in every Git repository on your computer.

You may already have a .gitignore file on your system. I found one in the folder I unzipped while installing Git. You can check if you have one by entering the following command in the terminal:

cd git-* && xdg-open .gitignore

If you get a “No such file or directory” error, then create a file there and then open it by using the following commands:

echo "" && xdg-open .gitignore

Now that we have the .gitgnore file open, add the following few lines to ignore any backup or swap files that Linux automatically creates.

*~
*.swo
*.swp

If you just created the entire .gitignore file from scratch, you may want to also copy and paste the code listed here for some other common things that are good to ignore.

Once that’s done, save and close the file and then run the following command back in the terminal:

git config --global core.excludesfile ~/git-*/.gitignore

That code will make sure that anytime you use Git on your machine, it will ignore any of the file extensions you just added to that .gitignore file!

Summary

So now you’re good to go with your Git install! You’ve added it to your Linux Virtual Machine, hooked it up to your GitHub account, and added some functionality to ignore unneeded file changes in your working directories…

All that’s left is to learn how to use it! ☺

Here are some resources I’ve found pretty helpful:

  1. Git Cheatsheet
  2. TryGit by Code School
  3. The official “Pro Git” book
  4. The Git Essentials class on Lynda.com

If you’re just getting started in Git and this guide helped, I’d love to hear from you! Enjoy Git!