Jump to content

Introduction to GIT


jje

Recommended Posts

Introduction to GIT

 

Hello there, and welcome to this beginners tutorial on GIT! A couple of months ago I was clueless about GIT and had never heard of it before, but recently (with the help of xaav, GitHub, and other sources) I have managed to learn the essential parts of GIT and wanted to share my experience with you. I've never written a tutorial on HelioNet before, so I hope this will help! :)

 

So, what is GIT?

GIT is a fast version control system commonly used to manage projects between a team of people. It allows you to create 'repositories' that store code, commits (changes), and issues. While GIT is a system that can be installed on a Linux server, GitHub is GIT Hosting that basically uses the GIT software to provide all the features that it does plus an easy to use web-interface to manage repositories; it's free unless you want to host Private Repositories. GitHub is intended to host Open Source software, and is used by over 1 million people and (at the time of writing) has nearly 3 million repositories. You can check out GitHub to understand more about Git, and the help pages they provide are really helpful.

 

First you'll need a server with GIT installed!

If you would like to use GIT on your own webserver, then you will need to install it first. Remember that if you are using GitHub then there's no need to set up your own GIT server; that's provided with GitHub. On a CentOS server, you can follow these steps to install GIT. You will need root access to SSH; let's go ahead and install GIT. You will need to install the EPEL repository on your system; there's a good tutorial at RidingTheCloud. If the EPEL repository is installed on the machine, run this to install git:

yum install git-core

Okay, you've now got GIT installed; congratulations! In order to take advantage of these, let's go ahead and install Gitolite. Go ahead and install Gitolite by running these commands via SSH:

adduser git
su - git
git clone git://github.com/sitaramc/gitolite
cd gitolite
src/gl-system-install

Well done; you have now successfully installed Gitolite :)

 

Now we need to setup our keys

In order to view and edit any GIT repositories, GIT will need to identify us in some way. In order to that, GIT uses keys called 'id_rsa'. To create these keys, you'll need to do a bit of work on your own personal computer.

 

Windows

Before we begin, go ahead and download Git from the msysgit coding website and run the setup. You may be prompted with a few things:

- You may be presented with a list of options on the 'Select Components' stage. Make sure that the 'Windows Explorer integration' is ticked, and 'Context menu entries' is selected but with the inner checkboxes unticked. GitHub has kindly provided a picture to help with this stage of the setup.

- You may be asked whether you would like to adjust your PATH environment; I recommend you choose 'Use Git Bash only'.

- You are likely to be prompted to configure the line ending conversions. Make sure that 'Checkout Windows-style, commit Unix-style line endings' is selected.

Now open the Git Bash application, and be ready to start configuring GIT.

 

Mac

Before we begin, go ahead and download Git from the official GIT website and run the setup. When the install is complete you will not see an application; it is built-in to a core application we're about to use now. To start using GIT, you will need to open an application called 'Terminal', which is located in Applications > Utilities. If you can't find it, you can also use Spotlight to find it.

 

So, shall we create some keys now?

Yes, let's begin. In the Git Bash or Terminal window you have open now, type in the following:

cd ~/.ssh

If you see a message that states that the directory does not exist, ignore these series of commands:

mkdir key_backup
cp id_rsa* key_backup
rm id_rsa

Now we're going to actually generate some keys. If you joined us from the point where you had the problem or .ssh not existing, welcome back. Run the following command, but replace yourname@yourdomain.com with your email address. You will also be asked to enter a passphrase (pretty similar to a password); this will prevent unauthorized use of GIT on your computer and you will be asked for it every time unless you use an ssh agent (this is available to Mac as standard).

ssh-keygen -t rsa -C "yourname@yourdomain.com"

Okay, you've now got your own key; congratulations! Now we've got to set it up.

 

What do we do know with those keys we just created?

What you did in the last chapter was create two very special files: id_rsa and id_rsa.pub. These are the identification files we talked about that GIT will use. Now we have to set it up with the server we are using.

 

If you are using GitHub, then find the id_rsa.pub file located in the .ssh directory inside your home directory. For example, if I was using Windows Vista/7 and my user account was called 'Administrator', my id_rsa.pub would be C:\Users\Administrator\.ssh\id_rsa.pub . Locate this file, and then go into editing mode with any editing application like Notepad (Windows) or TextEdit (Mac). Copy and paste the entire contents of the file into the text box located in "Account Settings" > "SSH Public Keys" > "Add another public key" on GitHub. When you've done, enter a Title (can be anything) and choose 'Add Key'. Voila, we're done adding your key :)

 

If you installed Gitolite on your own server, you will need to find the id_rsa.pub file located in the .ssh directory inside your home directory. For example, if I was using Windows Vista/7 and my user account was called 'Administrator', my id_rsa.pub would be C:\Users\Administrator\.ssh\id_rsa.pub . Once you have located the file, you will need to copy it to your server (for example, you can use FTP or SFTP) in the 'git' user home directory (in /home/git). Make sure it is chowned to the user 'git'; if you uploaded it under root or a user other than 'git' you will need to run:

chown git:git id_rsa.pub

Now that you have id_rsa.pub successfully on your server and belonging to GIT, you will need to rename it to something unique to you. For example, I could rename mine to jje.pub :

mv id_rsa.pub jje.pub

We can now tell Gitolite to start using your new key by running this command (replacing jje.pub to whatever you named your id_rsa.pub) - you should still be su'd to the 'git' user

gl-setup ~/jje.pub

 

Okay we've now got our keys sorted; now what?

We'll need to create a brand new repository for our code to go in. For GitHub users, you can use the interface at www.github.com to do this, while there is something a little bit more complicated for Gitolite users. Gitolite users will first need to clone the admin repository to edit it with new users. To do this, use Git Bash/Terminal on your computer to run these commands. This will create some files on your computer, so you might want to use 'cd' to change directory to a suitable clean location to avoid messing things up. Replace 'domain.com' with your server's domain name.

git clone git@domain.com:gitolite-admin

If that doesn't work, append a .git on the end. This will have created a new directory on wherever you changed the directory to named 'gitolite-admin'. Using a text editor, edit conf/gitolite.conf with your new configuration settings and press Save. If you specify a new repository in the gitolite.conf file (which you will do) Gitolite will automatically create the repo. More information on editing this file on the Gitolite doc page.

 

Woohoo; we're all setup!

Let's now take advantage of all our hard work by learning the simple Git commands. The clone command will clone the repository on the server onto your computer which allows you to work with it. For example:

git clone git@domain.com:myrepo

When you would like to pull the latest copy from the server, run the pull command while inside the directory to grab the latest copy and synchronize. You should always do this before starting to work on your code to avoid conflict.

git pull

Once you have made some changes, you will need to commit them. Before that, you can 'add' any brand new files you have created so GIT can recognize them

git add newfile.php

To commit, run

git commit -m 'My commit message. Replace this with your commit message.'

Alternatively for the add/commit method, you can also Commit All:

git commit -a -m 'My commit message. Replace this with your commit message.'

Once you have committed, you will need to push your changes.

git push

 

I hope this tutorial has helped. :)

Link to comment
Share on other sites

  • 4 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...