Monday, May 23, 2011

Git - Version Control System

An Introduction to GIT - 
Git is open source distributed version control system developed by Linus Torvalds to handle projects with great speed and efficiency ... Learn more

Scenario (what I have been working in) - 
I have a server of Ubuntu on Amazon EC2 and there I hosted one Django project on which I used to commit and deploy the code frequently.


Checking Out ... 

you need to generate an SSH key and set it up.
ssh-keygen
(You can leave everything to default when keygen asks you questions, thats the easiest way to go along)
Run this command next, and copy the output to clipboard or something..
cat ~/.ssh/id_rsa.pub

Now login to development server:
ssh -i amazonkey.pem ubuntu@myproject.bharat.in 

Then update the key as:
cd .ssh
cat >> authorized_keys
(mind the double redirection operator to append to the file)
paste the copied string, and you are good to go.

Next, clone from GIT.
git clone ubuntu@devel.bharat.in:/opt/repos/myproject.git

(Afterwards, you'll pull to get the latest changes as)
git pull origin master

You'll get a directory called myproject in your working directory, cd into it and fire..

python manage.py runserver

(That should be all to run the development instance)

Pushing/Commiting/Deploying etc etc

When you are done working, you may want to commit the changes..

git commit -a -m "Some message"

IMP TIP: If the message contains refs #ISSUEID, the commit will automatically be associated with the issue in its log,
if the message contains fixes #ISSUEID, the issue will be marked resolved,
and if the message contains closes #ISSUEID, the issue will be closed. *

Then you may push the changes to the server back as:
git push origin master

(All the commits from the last push in the branch master)

Finally to deploy, add a remote (one time)
git remote add stage ubuntu@myproject.bharat.in:/opt/repos/myprojectstage.git

All you have to do is to push into this remote's master as:
git push stage master

(The changes will immediately reflect at http://myproject.bharat.in)

Production remote:

git remote add production ubuntu@bharat.in:/home/ubuntu/repos/myprojectprod

Admin remote:

git remote add admin ubuntu@stage.bharat.in:/opt/repos/admin.git

Note for Beginners - 
Don't loose your heart if you don't have a Amazon EC2 or stuff like that ... you can start doing it using GitHub Please have a note that you have Git installed on any of the operating system you use. You can have git download "Jargon" at Git - Fast Version Control System.

# for this you need to first have a account on GitHub
# open git bash through start up launcher and check for .ssh if not then do 

ssh-keygen -t rsa -C "youremail@provider.domain"

# now come to .ssh and copy public key's content to github site

cat id_rsa.pub

# After doing it fine to check use this command 

ssh -T git@github.com

# If it works well then do it like 

git config --global user.name "FirstName LastName"
git config --global user.email "handle@provider.domain"

git config --global github.user githubusername (bindian0509)
git config --global github.token the_token_you_have_seen_on_github_website

# now go to the project directory which you have specified in the website (like mine "bharat")

cd bharat

git init

# to add a sample file

touch README.txt

git add -A (to add all the files present in your directory)
git add README (to add only readme)

git commit -a -m "Message"
git commit -m "Message"

# for adding a remote origin to github repos

git remote add origin git@github.com:bindian0509/bharat.git

# now push the code to 

git push origin master

# and now you are done with it