The beginners guide to the Git
This is a practical guide with the basic commands to use Git repositories. This will get you ready to start playing with the most popular version control system.
If you are new to the object oriented programming world (like me), you probably heard a lot about this website GitHub. My first interaction with it was through and interview to do some python pair programming. I created my user and repository and I thought -this site is like the “facebook” for programmers, cool!-. You can share your code with the community and users can comment on your projects and maybe help you with it.
After this python experience, I started to get more involved in web applications development and Github started to be everywhere I look. The real purpose of this repositories wasn’t totally clear to me yet, until the Ruby on Rails Tutorial by Michael Hartl fell in my hands.
In the first 2 chapters, he walk you through the Git repositories basics, how to use them, why you have to use them and what is the real purpose of all this.
I have some background as a programmer. Back in Argentina (where I’m from) I worked for 2 banks as a COBOL (Common Business Oriented Language) programmer, working with Mainframe terminals. I can’t say that I loved it. Banks use the Software Factory “layout” to create the different ranks in “the project assembly line”. This kind of set generates a lot of gaps where programmers doesn’t have anything to do, and that is really annoying!
Back to the version control system, in COBOL we used a system called Changeman where you have the three essential ambients to create a project: Development, Tests and Production. With the Git repositories you have this 3 environment too.
If you have played a little bit with databases you know how important is the magical spell Rollback. I would say, this is the main purpose of the version control systems. They give you the power to go back to and older version of your applications if any modification ruin your project. Also you have others powerful uses for this systems like sharing your repositories with your coworkers making it easy to split the tasks and then merge all the modifications or new routines together.
So lets gets busy with this. I divided the guide in:
- Install and Setup Git in your computer
- Create & Edit
- Merging changes
Install & Setup Git in your computer
This guide is for those who are using XOS or Linux, Windows is not really friendly when you want to program in Ruby
So first we need to install git on your computer. There are two ways to do it
1_ Using the default repositories on Ubuntu. The only problem with this method is you can’t pick the version of Git you will install
# Open a terminal and enter this two commands:
$ sudo apt-get update
$ sudo apt-get install git
This will download and install Git to your system.
2_ Installing Git from source (recommended)
Using this method allows you to pick the version of Git you will install
# First install the softwares that Git depends on.
$ sudo apt-get update
$ sudo apt-get install build-essential libssl-dev libcurl4-gnutls-dev libexpat1-dev gettext unzip
Now you have to visit the git project’s page on GitHub and get the version of Git you want to install.
To chose the version of Git, click on the button that says “branch: master”, then on the tag Tags you will find the different branches with the Git versions available.
Now that you have selected the Git version you need to download the components to install it.
Right click on the “Download zip” button and select “copy link address”.
Open a terminal window and use the following command.
# This command will download the git.zip for the installation.
$ wget https://github.com/git/git/archive/v2.2.1.zip -O git.zip
# Unzip the file and navigate into the directory created.
$ unzip git.zip
$ cd git-2.2.1
# Make the packages and install Git using this two commands.
$ make prefix=/usr/local all
$ sudo make prefix=/usr/local install
Alright! Git installed and running. To make sure that everything went right, check the version with this command. First close the terminal window and open a new one.
$ git --version
git version 2.2.1
To upgrade your Git to a later version you would need to clone the repository where the new version is allocated.
# Use this command to clone a repository. It will create a directory on the location where you are on your computer.
$ git clone https://github.com/git/git.git
You need to get the URL of the repository from GitHub.
# Then running this commands inside the new directory created before, will install the new version and overwrite the old one.
$ make prefix=/usr/local all
$ sudo make prefix=/usr/loca install
Setting up Git.
This part is easy, all we need to do is setup the user name and email used on GitHub to create your account.
# Enter this commands on a terminal
$ git config --global user.name (your user name)
$ git config --global user.email (your email address)
To see the configuration items use this command
$ git config --list
All the configuration setting are stored in a file that you could open with a text editor to modify.
$ gedit ~/.gitconfig
Connecting to GitHub, SSH key validation
This websites gives us two ways to connect to our repository.
1_ Connect to an https://.. URL. We can use this method but we will be prompt to enter our user name and password every time we update the repository, after a while it can be a little bit annoying.
2_ Using an SSH key. The SSH is a method to identify a computer with a pair of encrypted keys, a public part and a private. Basically what we want to do is add our public key into the website so when we try to connect, GitHub can recognize the device without asking us for the user name and password.
First, let’s see if we have any keys already in our system. We want to check if we have the directory ~/.ssh and list the content in it.
$ ls ~/.ssh
id_rsa id_rsa.pub known_hosts
If we have keys generated we are going to see the above list of files
id_rsa: private key
id_rsa.pub: public key, this is the one that we are going to share
known_hosts: It contains the public keys of the websites visited by the user, this is how the host know that is connected to the right computer.
If you already have this files all you need to do is copy the public key from “id_rsa.pub” and add it to the website that you want to connect.
$ cat ~/.ssh/id_rsa.pub
This command will display in your terminal the content of the file, so you will be able to copy the key. Go to GitHub, log in and enter in your profile page. Click on the button “Edit profile” and look for SSH keys on the list in the left side of the screen.
On the right top corner you will see the button “Add SSH key” click on it, create a title to identify your key and then in the text box under the title paste the public key that you copied before. Click on “Add key” button to confirm.
Generate a new SSH key
To generate a new key is really simple. Open a terminal and run this command
$ ssh-keygen -t rsa -C “firstname.lastname@example.org”
This will start the keygen app. Is going to ask you to enter the name of the file where you want to keep the keys, just press enter to generate the default directory and files.
Now that you have your new SSH keys just follow the steps above to add it to GitHub.
Finally to test that everything is working ok, use this command to connect to GitHub
$ ssh -T email@example.com
Hi cocolote! You’ve successfully authenticated, but GitHub does not provide shell access.
If everything went right, you will see a message like that one. If something is wrong just try to repeat the steps reading carefully.
Create & Edit
To start using Git, first we need to create a repository in the directory where you have your application. Navigate to your project and once you are in it use this command:
$ git init
This will create the repository with some information of your project, now we are ready to start using Git.
Next step is to create a repository on the website where you are going to keep your project. Go into your GitHub and create a new repository with the name of your application.
From this point the following commands don’t follow an specific order. You could use this part as a reference in the future.
# To add the project files into the repo (this will leave the the files in staging status, not added yet)
$ git add -A
With this command we initialize the repo.
# To submit the changes and confirm the files that we want to add.
$ git commit -m ‘Initialize repository’
The message is to identify later in the commit log what we were doing. Is a good habit to put messages with the commits. Is like leaving some bread crumbs for your self to keep track of the changes made in your project.
# Display the commit log
$ git log
# To revert any modification made it by mistake. This will work only if you didn’t commit the modification.
$ git checkout -f
“checkout” is a big command if you want to know more about it there’s a lot of documentation on Internet. StackOverflow has a lot of information about this command
# Check the status of your repository
$ git status
In the directory of your application you will find “.git” directory with the configurations of your repository. The file “config” has some variables and configurations to use in Git.
What we want to do is create a variable to keep the URL of our repository created on GitHub.
To get the URL log in on GitHub, navigate to your repository and in the right side of the screen you will see a text box “HTTPS clone URL” under the box you will have a text where you can choose which type of URL you want to use. Click on SSH and copy the URL.
# To add the repository URL into “config”.
$ git remote add origin firstname.lastname@example.org:user_name/repo_name.git
# To push your project to the remote repository on GitHub
$ git push -u origin --all
To edit something on your application is a good practice to create a new branch to do the modifications you want and try them. If everything is ok, you can merge the branch with the modifications to the “master” branch.
# Create a new branch
$ git checkout -b ‘modify-README’
# Show list of branches
$ git branch
Once you are done testing an modifying you app you will need to merge the modifications to the “master”
# To navigate back to the master branch
$ git checkout master
# To merge the modification on to the master branch
$ git merge modify-README
# To delete the branch used to do the modifications
$ git branch -d modify-README
You want to create branches so if the modification was a total disaster you could start over with the version of the application that you have in the master branch.
Now lets move the application to the test ambient. Heroku provides us with a work environment where we can test web applications and other stuff too. It use PostgreSQL as a database engine so remember to include the gem pg on your application before deploying.
First you will need to create an user on Heroku to be able to use the web service. Once you got it, you will need to install the Heroku toolbelt to be able to connect to Heroku. Finally you will need to add the public key like we did before.
To install the toolbelt just follow the instructions on the website.
# To connect to Heroku
$ heroku login
# To add the SSH public key
$ heroku keys:add
# To create a new repository on Heroku. It will create a subdomain available for immediate viewing
$ heroku create
The “create” command generates a random name for your repository so it would be better to rename it
# To rename the repo in Heroku
$ heroku rename the-name-of-your-repo
Then you will need to create a variable to access to the new repository that you created to push the project to the website.
# To generate the variable to your repository
$ git remote add heroku email@example.com:repo-name.git
# To push the application to heroku
$ git push heroku master
# List the history of releases
$ heroku releases
# To go back to a previous version in case of a new bug.
$ heroku rollback
The rollback command has the option to go back to an specific release version, you can learn more about this command on the website.
Well this will get you up and running to start practicing with the version control system Git. I hope this was helpful.