Setting up Git on CentOS

Git is a new version control system created by Linus Torvalds. If your starting a project, it is a good idea to have a version control system setup before starting the project. There are alternatives to git but two of the most popular ones are SVN, CVS. The major difference between git and the rest is that git is distributed where svn or cvs are server-client based.


Setup

By default, Git is installed on your CentOS, but even if it is not for some reason you can install it manually.
Note: all of the following commands require root privilege:

$ su root

execute the following command to install git:

$ yum install git

don’t worry if you don’t know whether or not Git is installed on your system, the above command will detect whether it is installed or not and will ignore the installation request.


Configuration

To setup Git on CentOS, first you need to setup a SSH server on CentOS. Click here to read “Setting up SSH on CentOS“.

First, we will create a new user named Git on the Server system:

 $ adduser git  

To set or even change the password:

 $ passwd git  

A Git user will be created and the home directory of git will be created in

 /home/git  

The owner and the group owner of that folder must change so it belongs to the new user ‘git’ – we can change that via:

 $ chown git:git /home/git  

if you followed the steps from my last post on how to create SSH server on CentOS you must also add the ‘git’ user to the SSH allowed list of SSH-ers configured in this file:

 /etc/ssh/sshd_config 

Now lets create the project repository. We will login as git (exit root):

$ exit root
$ su git

and create the repo in the home directory:

$ cd ~
$ mkdir ProjectNameRepo.git
$ cd ProjectNameRepo.git
$ git init –bare

This folder will be the git repository on your server for this particular project. This is the folder where you will get copies from (called clone-ing) and committing new updates (called push-ing) from different users.


Using the System

From a different computer where you will use to code, create the project folder and create a readme file inside the folder, then initialize it using git:

$ mkdir ProjectName
$ cd ProjectName
$ touch readme.txt
$ git init

for more information on git commands see references at the bottom.
stage the newly added files using git add:

 $ git add . 

Now we must commit, but before that you must configure the user via git config. Your name and e-mail will be used in as part of the committing metadata.

$ git config --global user.name 'Your Name'
$ git config --global user.email you@somedomain.com
$ git commit -m 'My first commit! Matt Damooooon'

The changes have been snapshot-ed and will get transferred to the repo when pushed. Now, we need to define the repo server which is the CentOS PC we configured on the first part of this walkthrough.

$ git remote add origin ssh://git@server-address:port/~/ProjectNameRepo.git

And now we can push the code and push it later on when changes are made to the code:

 $git push origin master 

To get a clone of the project on a different PC user the following command:

$ git clone ssh://git@server-name:port/~/ProjectName.git

and if you want to work on it from the local server, you can get a copy by specifing a different path when calling clone

$ git clone /home/got/ProjectName.git

In both cases, a copy of the repo will get copied to your current location under the directory name ProjectName without the .git extension.

References

A git reference based tutorial: http://gitref.org/creating/

This entry was posted in Linux and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>