Working with GIT

After a long time of avoiding the obvious flaw in my programming methodology I figured it was time to jump into using version control.  (Too many times have I broken code and had to spent ages trying to fix it).

Git was my choice to start out with as it has been recommended to me on several occasions. This is how I’ve got on so far.

After researching many articles on how to split up development code with “live” I have found that it is a lot simpler than I’d imagined, following these steps I had a functional console based git repo in minutes:

Basing this on CentOS which is what I’m using:

# yum install git

you could use git18 if you have the IUS repo installed (www.iuscommunity.com)

# useradd -m git
# su - git

Generate a ssh key based authentication for passwordless pushes later if you wish.

Set where you want to have your projects (/var/git is the default choice but I chose /home/git/projects)

# mkdir -p ~/projects/mytest.git
# cd ~/projects/mytest.git
# git init --bare

This will setup an empty repo

From here you work on the client side (whether it be on the same server, another server or on your home/work pc or laptop).

I used the same server when testing so in another user:

# mkdir ~/projects
# cd ~/projects
# git clone git@localhost:projects/mytest.git

Git will say that you are cloning an empty repository, this is fine.

If you already have code you want to submit to the repo paste it in here (be careful not to delete or overwrite to .git directory in the process).

If not you could just do a

# touch mytest

Now we want to add all of these files so do:

# git add .

“.” being the current working directory.

It should now mention that it has added the mytest file.

# git commit -am 'Initial commit of code'

-a flag means all files in the current directory and -m means use the following message to describe what has been done in this commit.

# git push origin master

This will now push your files (or mytest file) to the remote repository (origin).

Since the master is the “live” working version you can branch out with development branches:

# git branch

This will list your current branches

#git branch development

This will create a branch called “development”.

# git checkout development

Now you are in the development branch on your local machine.

If you want to create the branch on the git repo server you can use

# git push origin development

When your code is ready to be moved to live, merge the branches by first moving to the destination then merging:

# git checkout master
# git merge development

Now the content of master will be the same as development.

This doesn’t go into advanced techniques but will get you setup with a basic repo and a head start.