How to setup a Git repository

Would you like to share some of your solutions for certain problems? Tips or tricks? Post here. All new topics are automatically moderated.

How to setup a Git repository

Postby dennylin93 » 30 Jan 2010, 07:52

This howto will describe how to setup a Git repository:
  • Dedicated user for Git repos
  • SSH will be used for commits
  • Enable gitweb for web access (Apache will be used)
  • Anonymous cloning using the Git protocol

For those who don't know what Git is:


You should know how to use Git before reading on.

Install [port]devel/git[/port]. Select [file]GITWEB[/file]. [file]SVN[/file], [file]P4[/file], and [file]CVS[/file] are optional. Deselect them if you don't plan to use them.

Create a git user with uid and gid as 9418:
Code: Select all
# pw groupadd -n git -g 9418
# pw useradd -n git -u 9418 -g git -c git -d /git \
   -s /usr/local/libexec/git-core/git-shell -h -


The [file]git-shell[/file] is used for this user, and home is set as [file]/git/[/file]. The repos will be under [file]/git/base/[/file].

Make sure the permissions of the directory are correct and create [file]/git/base/[/file]:
Code: Select all
# chown git:git /git/
# chmod 755 /git
# mkdir /git/base/
# chown git:git /git/base/
# chmod 775 /git/base/


Next, add users to the git group to be able to create repositories under [file]/git/base/[/file]. This isn't necessary for users who only need commit access.
Code: Select all
# vi /etc/group
...
git:*:9418:user1,user2


We'll be using SSH keys for authenication, so collect the public keys of all the users who need commit access. Then, put the public keys into the right place:
Code: Select all
# mkdir /git/.ssh/
# chmod 700 /git/.ssh/
# touch /git/.ssh/authorized_keys
# chmod 600 /git/.ssh/authorized_keys
(Put the public keys into authorized_keys, one per line)
# chown -R git:git /git/.ssh/


Everything should be set now. Let's create a repo for testing (change to a user that has been added to the git group and has commit access).
Code: Select all
$ mkdir /git/base/test.git
$ cd /git/base/test.git && git init --bare --shared


Create a local repo and commit:
Code: Select all
$ mkdir ~/test
$ cd ~/test && git init
$ echo '123456' > foo
$ git add .
$ git commit


Now push it into the remote repo (remember to replace git.example.com with your own hostname):
Code: Select all
$ git remote add origin git@git.example.com:base/test.git
$ git push origin master


Don't delete the test repo yet.

Since the git repo should be up and working by now, let's enable gitweb for web access. Apache's VirtualHost will be used for this.

Copy gitweb files to [file]/home/www/git/[/file]:
Code: Select all
$ cp /usr/local/share/examples/git/gitweb/git* /home/www/git/


Modify Apache settings (change the ServerName and the access and error log paths as necessary):
Code: Select all
<VirtualHost *:80>
  ServerAdmin webmaster@yourhostname
  DocumentRoot "/home/www/git"
  ServerName git.example.com
  ErrorLog "/path/to/errolog"
  CustomLog "/path/to/accesslog" combined

  <Directory "/home/www/git">
    Options ExecCGI
    Order allow,deny
    Allow from all

    DirectoryIndex gitweb.cgi
    AddHandler cgi-script .cgi
  </Directory>
</VirtualHost>


Now edit [file]gitweb.cgi[/file]:
Code: Select all
-our $projectroot = "/pub/scm";
+our $projectroot = "/git/base";
...
-our $home_link_str = "projects";
+our $home_link_str = "base";
...
-our $site_name = ""
+our $site_name = "git.example.com"
...
-our $home_text = "indextext.html";
+our $home_text = "content.html"; (Leave empty if unnecessary)
...
-our $projects_list_description_width = 25;
+our $projects_list_description_width = 40; (Give the description a bit more space)


Change the $site_header, $home_text, and $site_footer as needed.

Open up your browser and check if it's working.

You might notice that the description of the test repo hasn't been modified yet. You might also want to change the owner.

For the description, edit [file]/git/base/test.git/description[/file]. Put this into [file]/git/base/test.git/config[/file] to change the owner:
Code: Select all
[gitweb]
        owner = Your Name


Only the Git protocol isn't working now.

Add this to [file]/etc/rc.conf[/file]:
Code: Select all
git_daemon_enable="YES"
git_daemon_directory="/git"
git_daemon_flags="--syslog --base-path=/git --export-all"


Start the daemon:
Code: Select all
# /usr/local/etc/rc.d/git_daemon start


You should now be able to clone using the Git protocol. Try it out:
Code: Select all
$ cd /tmp/
$ git clone git://git.example.com/base/test.git


One last thing. You might want to list URLs for cloning repos on the summary page of test.git in gitweb. Just add this line (the url line) to the [file][gitweb][/file] section of [file]/git/base/test.git/config[/file]:
Code: Select all
[gitweb]
        owner = Your Name
        url = git://git.example.com/base/test.git


This line can appear more than once if there are multiple URLs:
Code: Select all
[gitweb]
        owner = Your Name
        url = git://git.example.com/base/test.git
        url = git@git.example.com:base/test.git
dennylin93
Member
 
Posts: 784
Joined: 11 Dec 2008, 13:13

Postby VictorGT » 21 Oct 2010, 14:53

Thanks a lot for this useful article!

I've noticed one problem on my FreeBSD 7.2 - looks like it is better to add one more flag ([FILE]--detach[/FILE]) to the rc.conf:
Code: Select all
git_daemon_flags="--syslog --base-path=/git --export-all --detach"
VictorGT
Junior Member
 
Posts: 1
Joined: 21 Oct 2010, 14:47

Postby graudeejs » 21 Oct 2010, 15:35

You can use [port]devel/py-gitosis[/port] to manage git users.
This way system only needs 1 git user. Other users will be virtual users (authorization with ssh public/private keys)

It is very nice peace of software...
I use gitosis & cgit at git.bsdroot.lv
User avatar
graudeejs
Style(9) Addict
 
Posts: 4591
Joined: 16 Nov 2008, 23:23
Location: Riga, Latvia

Postby blaize » 25 May 2012, 19:05

VictorGT wrote:Thanks a lot for this useful article!

I've noticed one problem on my FreeBSD 7.2 - looks like it is better to add one more flag ([FILE]--detach[/FILE]) to the rc.conf:
Code: Select all
git_daemon_flags="--syslog --base-path=/git --export-all --detach"


I have the same problem on FreeBSD 9, if you don't add [file]--detach[/file] in [file]rc.conf[/file], the daemon is started in the foreground and the boot process is stuck at launching the daemon (before SSH :/)

Except that, everything works very well ! Thank you.
blaize
Junior Member
 
Posts: 22
Joined: 25 May 2012, 19:01


Return to Howtos & FAQs (Moderated)

Who is online

Users browsing this forum: No registered users and 1 guest