Git

Материал из Webko Wiki
Перейти к навигации Перейти к поиску

First start working from your pc ,not from git server.

First start with creating RSA key in your system means your client machine (not git server).We will start working in Git Server after finishing steps in client PC.

Step A: login into your linux pc from where you want to manage git server remotely .(means here I am talking about your client system) Now create the RSA key . It will create two files id_rsa and id_rsa.pub in your system’s $HOME/.ssh directory. Do not give passphrase password. Just hit enter when it ask for setting passphrase password


$ ssh-keygen -t rsa -C "Git-Admin"

Step B: Now scp the id_rsa.pub file into Git Server (IP Address : 192.168.169.95)


$ scp ~/.ssh/id_rsa.pub root@ip-address-of-git-server:~ 


Login into the GitServer with user root.

Step 1: Install git,http and perl dependency.


# yum -y install git httpd perl-Time-HiRes

Step 2: Create user git and change its gid and uid


# useradd git
# usermod -u 600 git
# groupmod -g 600 git

Step3: Rename the id_rsa.pub file which you did scp from your system.And move the rename files to /home/git and change its owner & group with git


# mv /root/id_rsa.pub /home/git/Git-Admin.pub ; chown git:git /home/git/Git-Admin.pub

Step 4: Login with user git directly from terminal.Do not set password for user git. And git clone the gitolite from github.com


#su -l git
$whoami          (The command will show you log in with which user)
$echo $HOME       (The command will show what is your home directory)

$ git clone git://github.com/sitaramc/gitolite

Step 4: Create bin directory in /home/git and setup the Git-Admin account. Note: We renamed the id_rsa.pub file to Git-Admin.pub ,the reason is the Gitolite will provide same name of user in gitolite.conf file as the name of .pub file. for eg. if I use only id_rsa.pub then “id_rsa” user will be created.Hence if you are adding any user in Git Server by using its ssh key, rename the user’s id_rsa.pub key with user-name.pub . like joe’s rsa file should be change into joe.pub (id_rsa.pub –rename–> joe.pub)

$ mkdir -p /home/git/bin
$ gitolite/install -ln
$ gitolite setup -pk Git-Admin.pub

Step 5: Exit from user git login and again use the user root. Now check what are values defined for suexec bydefault. Use the command “suexec -V”


$exit   (logout from git user)

you should be now root if you are following the steps as it is.If no then no-problem login with root again :)

# suexec -v 

Given below is my server’s suexec details.

[root@gitserver ~]# suexec -V
-D AP_DOC_ROOT="/var/www"
-D AP_GID_MIN=100
-D AP_HTTPD_USER="apache"
-D AP_LOG_EXEC="/var/log/httpd/suexec.log"
-D AP_SAFE_PATH="/usr/local/bin:/usr/bin:/bin"
-D AP_UID_MIN=500
-D AP_USERDIR_SUFFIX="public_html"

Step 6: Create a directory bin in /var/www (Why /var/www ? because I got the detail from suexec -V,see parameter AP_DOC_ROOT) Below given command will create directory bin in /var/www with permission 0755 and owner &group is git


#install -d -m 0755 -o git -g git /var/www/bin

Step 7: Create a bash script with name gitolite-suexec-wrapper.sh in /var/www/bin/. Then save and exit.(I am using vi editor,you can choose your any other editor)


vi /var/www/bin/gitolite-suexec-wrapper.sh

#!/bin/bash
#
# Suexec wrapper for gitolite-shell
#
export GIT_PROJECT_ROOT="/home/git/repositories"
export GITOLITE_HTTP_HOME="/home/git"
exec ${GITOLITE_HTTP_HOME}/gitolite/src/gitolite-shell
Step 8: Set ownership and permission of /var/www/bin and gitolite-suexec-wrapper.sh
# chown -R git:git /var/www/bin
# chmod 750 /var/www/bin/gitolite-suexec-wrapper.sh
#chmod 755 /var/www/bin

Step 9: Now change the value of “UMASK 0077,” into “UMASK => 0027,”in file /home/git/.gitolite.rc


vi /home/git/.gitolite.rc

UMASK => 0027,

Step 10: Install GitWeb


yum install gitweb

Step 11: By-Default the gitweb will be installed in /var/www/git directory.(It has gitweb.cgi file) We will rename git directory as gitweb. And move the renamed directory inside /var/www/html

# mv /var/www/git /var/www/html/gitweb

Step 12: Change the ownership of /var/www/html/gitweb


# chown -R git:git /var/www/html/gitweb

Given below are the details of my server


[root@gitserver html]# chown -R git:git gitweb/
[root@gitserver html]# ls -ld gitweb/
drwxr-xr-x 2 git git 4096 Jun  1 12:36 gitweb/
[root@gitserver html]# ls -la gitweb/
total 252
drwxr-xr-x 2 git  git    4096 Jun  1 12:36 .
drwxr-xr-x 3 root root   4096 Jun  1 12:34 ..
-rw-r--r-- 1 git  git     115 Apr 24  2010 git-favicon.png
-rw-r--r-- 1 git  git     207 Apr 24  2010 git-logo.png
-rwxr-xr-x 1 git  git  204754 Jun  1 12:36 gitweb.cgi
-rw-r--r-- 1 git  git    8379 Apr 24  2010 gitweb.css
-rw-r--r-- 1 git  git   24142 Apr 24  2010 gitweb.js
[root@gitserver html]#

Step 13: Edit the file /etc/gitweb.conf. and change the value of two variables

$projectroot and $projects_list as given below
vi /etc/gitweb.conf
    our $projectroot = "/home/git/repositories/";
    our $projects_list = "/home/git/projects.list";

Step 14: Edit the file /var/www/html/gitweb/gitweb.cgi and change the value of two variables $projectroot and $projects_list as given below

vi /var/www/html/gitweb/gitweb.cgi

   our $projectroot = "/home/git/repositories";
   our $projects_list = "/home/git/projects.list";

Step 15: Create dummy folder git . And be carefull with permissions,owner and group


#install -d -m 0755 -o apache -g apache /var/www/git  (This is dummy one)

Step 16: Open the /etc/httpd/conf/httpd.conf file and in last line paste the given below VirtualHost Configuration.

Note: If you have set the hostname and FQDN of git server then you can uncomment the ServerName,ServerAlias related lines and write the information which you have. If you do not have these info ,no problem paste the content as it is.

<VirtualHost *:80>
  # You can comment out the below 3 lines and put correct value as per your server information
  #  ServerName        gitserver.example.com
  #  ServerAlias       gitserver
   ServerAdmin       [email protected]

   DocumentRoot /var/www/git
   <Directory /var/www/git>
       Options       None
       AllowOverride none
       Order         allow,deny
       Allow         from all
       
   </Directory>

   SuexecUserGroup git git
   ScriptAlias /git/ /var/www/bin/gitolite-suexec-wrapper.sh/
   ScriptAlias /gitmob/ /var/www/bin/gitolite-suexec-wrapper.sh/

   <Location /git>
       AuthType Basic
       AuthName "Git Access"
       Require valid-user
       AuthUserFile /etc/httpd/conf/git.passwd
   </Location>
</VirtualHost>

Step 17: Edit the /etc/httpd/conf.d/git.conf file. After installation of GitWeb this git.conf file created itself. Here we are doing some changes as per our Git Server. Carefully see each line which I edited. Do not skip it is important part also.

vi /etc/httpd/conf.d/git.conf 

Alias /gitweb /var/www/html/gitweb
<Directory /var/www/html/gitweb>
 Options +ExecCGI
 AddHandler cgi-script .cgi
 DirectoryIndex gitweb.cgi
</Directory>
<Location /gitweb>
  AuthType Basic
  AuthName "Git Access"
  Require valid-user
  AuthUserFile /etc/httpd/conf/git.passwd
</Location>

Step 18: Now we will create Basic apache auth username and password. When you create a first time a user then only we use “-c” flag. -c means create new file. see man page of htpasswd.

# htpasswd -c /etc/httpd/conf/git.passwd admin

For new user addition or updating existing user’s password do not use -c flag

 # htpasswd /etc/httpd/conf/git.passwd  user1
 # htpasswd /etc/httpd/conf/git.passwd  testuser

Restart/Reload the apache whenever you setup a htpasswd user or passwd chkconfig command to set apache service to run in runelevel 3 and 5 whenever system start.


#/etc/init.d/httpd restart;chkconfig httpd on

All configuration is done.Now Git Server is ready to use.

How to see GitWeb webpage

Open the web browser and in URL type (give username and password which you set at htpasswd command)

http://ip-address-of-git-server/gitweb/

How to clone the repository through HTTP in client machine

Use the command. (give username and password which you set at htpasswd command)


$git clone http://ip-address-of-git-server-OR-FQDN/git/repo-name.git

See difference, when I am cloning the repo from Git Server I am using word git in between of URL. But for git web ,I am using word “gitweb”

Note: If you want to learn why git and gitweb,open the git.conf and httpd.conf file.

In httpd.conf , “ScriptAlias /git/ /var/www/bin/gitolite-suexec-wrapper.sh/ ”
In git.conf “Alias /gitweb /var/www/html/gitweb ”
$ cd ~/Desktop
$git clone http://ip-address-of-git-server-OR-FQDN/git/testing.git

Now it will clone the testing repo in your Desktop or wherever directory from you run the git clone command

How to Manage user and group of Git Server

For managing Users/Groups in Git Server from your remote machine, you have to clone the gitolite-admin repo in your System. But it is the same system from which you scp the RSA key file.(Read Step A and Step B ,start of this post)

Here I will clone gitolite-admin in Desktop

$cd ~/Desktop
$ git config --global user.name "Git-Admin"
$ git config --global user.email "[email protected]"
$ git clone git@GitServerIP-or-FQDN:gitolite-admin.git

With the help of gitolite.conf file you can manage the Users and Group from your system. But whenever you do any changes you have to do “git push”

Given below is reference of my system

sharad@mypc:~/Desktop/gitolite-admin/conf$ pwd
/home/sharad/Desktop/gitolite-admin/conf
sharad@sharad-sapplica:~/Desktop/gitolite-admin/conf$ cat gitolite.conf
repo gitolite-admin
RW+ = Git-Admin
repo testing
RW+ = @all
R = git daemon
sharad@mypc:~/Desktop/gitolite-admin/conf$
R = Read
W = Write

How to push new changes of gitolite.conf into git server from my system

Use the given below command to push the changes into Git Server.

Go to gitolite-admin cloned directory and change to conf directory also. Edit the changes in gitolite.conf and push the code with given below command.

$ cd ~/Desktop/gitolite-admin/conf
$ls -l gitolite.conf

$git add gitolite.conf
$git commit -m "first commit"
$git push origin master

Note: “git pull” command is for pull the Version files/directory from Git Server.

How to create repository in Git Server In this eg. we are creating repo called “test-repo”

Login in Git Server as root.And then change to git user.

# su -l git
$ cd repositories
$ mkdir test-repo.git
$ cd test-repo.git
$ git --bare init
$ git update-server-info