Sunday, 26 July 2015

GIT, Gitosis, GitBlit Installation in Centos/RHEL 6

1. Using Apache authentication

yum install git
useradd -s /sbin/nologin -d /data/gitroot git
chmod 2770 /data/gitroot/

#cd /var/www/html
#ln -s /data/gitroot gitroot

#vim /etc/httpd/con/httpd.conf

<VirtualHost *:80>

        # You can comment out the below 3 lines and put correct value as per your server information
        DocumentRoot /var/www/html/gitroot
<Location />
        DAV on

#        # general auth settings
        AuthName "Git login:"
        AuthType Basic

#        # file authentication
        AuthUserFile  /etc/httpd/git/htpasswd.git

@@@@@Creating Repository and give access to user@@@
1. #mkdir /data/gitroot/reponame
2. #cd /data/gitroot/reponame
3. #git --bare init
4. #chown apache.apache -R /data/gitroot/reponame
5. #sudo -u apache git update-server-info
6. # Add below lines end of file /etc/httpd/conf/httpd.conf
    <Location /reponame>
        <LimitExcept PROPFIND>
            Require valid-user

7. Create username for access
   #htpasswd /etc/httpd/git/htpasswd.git username
8. #service httpd reload

@@@Check repo from client machine by clone, add, commit, push@@
#Run below from client machine
#mkdir live
#cd live
#git clone
#cd reponame/
#echo "Test file created" > test.txt
#git remote add originServer git@
#git add test.txt
#git commit -m 'added test.txt'
#git push origin master

2. Git Using SSH authentication Using Keypair authentication

Install Gitosis

   1. Download Gitosis from (or git clone git:// )
   2. Install it by
       cd gitosis
       python install
       If you get errors, try yum install python-setuptools

Generate SSH public/private key (If you don't have one)

   1. To generate a public/private key, run
       ssh-keygen -t rsa
   2. The newly generated public key will be at $Home/.ssh/ directory and named Private key will be named id_rsa.
   3. Upload your public key to server's /tmp directory
       scp ~/.ssh/ username@SERVER_IP_ADDRESS:/tmp/

Configure Gitosis

   1. Create a user to own the repositories, usually called git
       adduser git
   2. Create the repository and add YourPublicKey into authorized keys
       sudo -H -u git gitosis-init < /tmp/

Configure the permissions

   1. To configure who can access to your repositories, clone gitosis-admin first and then edit gitosis.conf in gitosis-admin directory
       git clone git@SERVER_IP_ADDRESS:gitosis-admin.git
       cd gitosis-admin
       vi gitosis.conf
   2. Add a repository, TestProject, and make user Howard has full access to it.
       [New group]
       members = Howard
       writable = TestProject
   3. Get Howard's public key,, from Howard and put it into keydir of gitosis-admin
       cp keydir
   4. Once done, commit it and push to remote server.
       git commit -m "Give Howard full access to TestProject"
       git push

Create the repository TestProject

   1. User Howard creates the TestProject, initialize it and push it to remote server
       mkdir TestProject
       cd TestProject
       git init
       git remote add originServer git@SERVER_IP_ADDRESS:TestProject.git
       // create files, git add . and commit
       git add .
       git commit -m "Initial commit"
       git push originServer master:refs/heads/master
   2. Now TestProject is created and available on server
   3. To allow user Sibo accessing TestProject, edit gitosis.conf of gitosis-admin and add Sibo to members of [New group] and put Sibo's public key into keydir of gitosis-admin
   4. Commit it and push it remote server and now Sibo will be able to clone TestProject from remote server
       [@Sibo Local]
       git clone git@SERVER_IP_ADDRESS:TestProject.git
3. GIT Using  SSH password authentication
Install git.
[root@svn ~]# yum install git
Add the developers group, all git users will be part of this group.
[root@svn ~]# groupadd developers
Create the git user which will own all the repos.
[root@svn ~]# useradd -s /sbin/nologin -g developers git
[root@svn ~]# passwd git
Changing password for user git.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
Update Permissions.
[root@svn ~]# chmod 2770 /home/git/
Create an empty Git repo.
[root@svn project1]# git init --bare --shared
Initialized empty shared Git repository in /home/git/project1/
Update file ownership and permissions.
[root@svn project1]# chown -R git .
[root@svn project1]# chmod 2770 /home/git/project1
Create a git user account.
[root@svn git]# useradd -s /usr/bin/git-shell -g developers -d /home/git tony
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
[root@svn git]# passwd tony
Changing password for user tony.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
At this point a regular user should be able to checkout the project1 repo from the Git server.
tony@apha05:~$ mkdir ~/testing_shit/git_test
tony@apha05:~$ cd ~/testing_shit/git_test && git init
tony@apha05:~/testing_shit/git_test$ git remote add origin tony@svn:/home/git/project1
Interestingly enough, an initial first commit has to be made onto the repo in order for any regular user to be able to push the repo, ie master branch. I received the following error when trying do so.
tony@apha05:~/testing_shit/git_test$ git push origin master
tony@svn's password:
error: src refspec master does not match any.
error: failed to push some refs to 'tony@svn:/home/git/project1'
tony@apha05:~/testing_shit/git_test$ git commit -m 'Initial'
[master (root-commit) 7bb7337] Initial
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README.txt
tony@apha05:~/testing_shit/git_test$ git push origin master
tony@svn's password:
Counting objects: 3, done.
Writing objects: 100% (3/3), 209 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To tony@svn:/home/git/project1
* [new branch] master -> master

4. GITBlit Installation

Step 1 # Pre-requists packages & Install gitblit

1. yum install java-1.7.0-openjdk
2. mkdir /opt/gitblit
3. cd /opt/gitblit
6. cp /etc/init.d/gitblit
7. vim /etc/init.d/gitblit modify the below line then save

@@Run the below commands only if you need to store repositories in different location@@

By Default repositories will be created under /opt/gitblit/data/git [called basefolder] If you want to change this path
execute below commands.

Example path for basefolder: /Newdata/gitroot

#mkdir /Newdata/gitroot
#mv /opt/gitblit/data /Newdata/gitroot/
#cd /opt/gitblit/
#ln -s /Newdata/gitroot/data data

8. service gitblit start
9. netstat tupln | grep LISTEN | grep java

tcp 0 0 :::1337 :::* LISTEN 30573/java
tcp 0 0 :::8443 :::* LISTEN 30573/java
tcp 0 0 :::29418 :::* LISTEN 30573/java
tcp 0 0 :::9418 :::* LISTEN 30573/java

10. Go and Browse http://GIT_SERVER_IP:1337
username: admin
password: admin

How to create Repository:

Goto Gitblit Portal --> repositories --> new repository -->

1. Enter the repository name in name tab

Note : Project should be selected as /
2. Add description about repository
3. Select Access Policy to : Restrict View, Clone, & Push [4th Option]
4. Ticket allow forks
5. Click Create


No Go to your local machine

# Go to Local Machine
# yum install git [Git version should be >= 1.7.10 ]
# git add .
# git commit -m 'commited first time'
# git push origin master

How to create Users:

1. Goto --> admin user preference --> Users --> new user

username: Enter username [ex: mansoor]
password: Enter password
confirm password: Enter password again
displayname: Enter Display name of user [ex: Mohamed Mansoor]
email address: Enter email address of user
@@@Dont click any options like (can admin, can create, can fork, exclude from federation, disable user)@@@
Click Save

2. Goto --> admin user preference --> Users --> click on ther username

Go to access permissions select the Reponame & Select the required access (R & RW+)

How to Create Branch:
#Clone the Repository: Repo name = commitcheck#
git clone
#To View the Branches#
git branch
#To add new bracnh#
git branch newbranch
#To View the Branches#
git branch
#To push the branch into Repository#
git push origin newbranch
#To Switch to the branch newbranch#
git checkout newbranch
#Add one file for testing#
vim newbranch1.txt
#Add the file to git#
git add newbranch1.txt
#Give comments for commit#
git commit -m 'added newbranch1.txt'
#Push the file to branch newbranch#
git push origin newbranch
#To Delete Branch newbranch#
git push origin --delete newbranch

TAG Creation
Create and push file into branch newbranch
touch cms1.txt
git add cms1.txt
git commit -m 'added cms1.txt'
git push origin newbranch
#Find the commit id of pushed file by using git log#
git log
#Set the tag for above pushed file's commit ID#
git tag 1.0 f9a52e6d4ba8b58925701df801ed6f1df3ad4943
#Push the all tags to Master branch#
git push --tags origin

GitBlit Commit Email Notification

Step 1# vim /opt/gitblit/data/
#Modify below lines as mentioned below
mail.server = localhost
mail.fromAddress =

Step 2# service gitblit restart

Step 3# Login to Gitblit Admin Dashboard
Goto Gitblit Dashboard -->Edit Repository -->
receive --> post-receive scripts --> Move the sendmail-html from Available to Selected -->Save
miscellaaneous --> add email id in mailling lists, for multiple add with space (

# cd git-1.8.3
# ./configure –prefix=/usr/local/git1.8.3
For HTTP : /usr/local/git1.8.3/bin/git clone
For SSH: /usr/local/git1.8.3/bin/git clone ssh://USERNAME@
git add .
git commit -m 'testfile added'
git push origin master

Reference Links:

1 comment: