Saturday, May 17, 2008

Incompetent git!! Or is it?

Git used to be, in my vocabulary, a word I use when I'm angry with someone. Now it's a new word I use to keep my codes version controlled and backed up. Interesting how language can run circles around you

If you don't know what git is, here's a detailed explanation on Wikipedia

Here's a brief tutorial on how to use git to store your development codes. At the end of this tutorial we provide a link for you to download a script we created to automate push and pull from our git server. Note that we're running this on an Ubuntu server (gutsy) and using Ubuntu desktop (gutsy/hardy) on our computers. Now, if you're using Microsoft for doing Ruby on Rails – seriously? (git is possible in Microsoft. You can find git tutorials for Microsoft on the web :) )

1. First, install git in your system/server. Open a terminal in your desktop and type the following:
sudo apt-get install git-core

2. Then, create a folder, lets call it myfirstapp
mkdir myfirstapp

3. Enter the directory that you created
cd myfirstapp

4. Initialize the directory created to be git aware
git init

5. Now you have completed initializing your local directory. Test the setup by doing the following:
Create any file in the folder
touch file1

To display the new file created,use this command
git status

It should display something like this
# On branch master
# Initial commit
# Untracked files:
# (use "git add ..." to include in what will be committed)
# file1
nothing added to commit but untracked files present (use "git add" to track)

which means you have succeeded in creating the repository.

6. Commit the file to your local repository.
git add . (Make sure you add the dot in after add)
git commit -m “put your message here”

7. Now, create your remote repository based on your local repository.
git remote

Nothing should display at this stage
git branch -a

Now, it should display something like this
* master

8. Create a remote connection to your centralized server. We use secured shell (ssh) to create a remote connection to our server.
ssh -l foo
enter the password for foo

9. Create a folder in your home driectory
foo@foobar:~$mkdir myfirstapp.git

Enter the directory created
cd myfirstapp.git

10. Initialize the directory by typing :
git --bare init

Exit from your remote connection:

11. In case you are not in the repository folder that you have created earlier,change to that directory in your local machine. Add the origin of the remote by typing
git remote add origin ssh://

12. Verify by viewing the the .git/config file
cat .git/config

You should see something like this
[remote "origin"]
url = ssh://

13. Push the changes to the server
git push origin master

14. Edit your .git/config using your favourite editor (our 'engineers' use vi. You can always choose to use nano or gedit )
vi .git/config

Now, add the following line
[branch "master"]
remote = origin
merge = refs/heads/master

15. Quit and Save.

You have now created a repository for your codes in your remote server. Now you need to set up your local directory to automatically link to the remote repository to pull codes. Your team mates will need to do the following too to get a copy of the codes (and git setup!). To get the source from the repository, the first step is to clone the source code. In your projects folder, (parent to myfirstapp, for example), type in the following:
git clone ssh://

This will clone your codes from your remote server to your local machine

The next step would be to set the .gitignore files. This file specifies what files to ignore when pushing to the remote server. You would not want to push your log file and other insignificant files to your remote server, won't you? Use your favourite editor to edit .gitignore. Again, by using vi,
vi .gitignore

Copy and paste the following lines

These are the file types that you may not want to push to the remote repository. Your requirements may vary, so make sure!. Once you've specified the file types to ignore, save and quit the text editor.

You are now all set to pull and push from your server. The following steps detail how you should ALWAYS pull and push to your server

1. First,use the following commands to add your files to your local repository.

git add . (Again, make sure the dot after “add”)
git commit -m “your message here”

2. Next, pull the codes from repository from the remote server in order to avoid any conflicts with existing codes in the remote server (your team mates might have upload codes before you, so they might be in conflict with changes you made to the codes)
git pull

If something is wrong – a conflict, for example, you and your team mate both modified the same lines of code - fix it first.

3. If there are no problems, then you may proceed with pushing your codes to the remote server
git push

Now, the server is updated with your new codes, and others may pull your codes from the repository.

Now as programmers we know very well that it's easy to overlook one or two of the procedures mentioned above – especially if you're pushing code at around 2-4 am – so we created a script for us to push our codes without having to bother doing all the above. Oh, did I forget to mention that we're also lazy?

If you want to you can download the script from here. Remember to remove the .txt extension before using (Scribd quirk, can't upload if they don't know your file format) Note that we won't be held responsible if anything happens to your data or system (In other words, make sure you know the ins and outs of your system and the script before you use it!)

Here are the steps for you to run our script

1. Save the script to the local repository folder in your machine. In this case it would be the myfirstapp folder.

2. Change the file access to executable file
chmod +x pullpush

3. Run the script from the local repository folder

Follow the instructions provided as the script runs (enter passwords etc). You may terminate the script by using ctrl+c (Hey, it ain't perfect, but it works)

That's it for our brief git tutorial! Learn to do it, and you can avoid becoming an actual git yourself!

1 comment:

fadhli said...

Nice write up on git. Glad that everybody is gitting it!