-
Notifications
You must be signed in to change notification settings - Fork 19
Git Local Repository Setup Guide
Rami Al-Ghanmi edited this page Oct 24, 2013
·
15 revisions
The goal of this guide is to show you how to setup a local git repository with a web interface on your local machine. The process involved creating a shared git user account to access the repository. The account is shared in the sense that all repository users will use it to push/pull from the server, but git is setup to give each user credit for their work.
- First, we create the git user and set the account up for SSH Public Key Authentication and no terminal login. That means, the git account can not login using a password (only via PKA) and has no regular shell access. Instead, it will be using a special git shell with a limited set of commands
#Create git user account
sudo adduser --shell $(which git-shell) --gecos 'git version control' --disabled-password git
#Add git user to the appropriate groups
sudo usermod -a -G www-data git
sudo usermod -a -G developers git
#Setup authorized_keys file for access
sudo mkdir -p /home/git/.ssh
sudo touch /home/git/.ssh/authorized_keys
sudo chmod 600 /home/git/.ssh/authorized_keys
sudo chmod 700 /home/git/.ssh
#Copy the git-shell-commands to get limited shell access
sudo cp -r /usr/share/doc/git/contrib/git-shell-commands /home/git/
sudo chmod 750 /home/git/git-shell-commands/*
#Fix permissions
sudo chown -R git:git /home/git/
- Add your SSH key to the authorized key list. You can repeat this step for all users you wish to give access to. If you have yet to generate a key-pair, follow steps 1-3 in this article
cat ~/.ssh/id_rsa.pub | sudo tee -a /home/git/.ssh/authorized_keys
- Allow the
git
user to access the system via SSH
echo "AllowUsers git" | sudo tee -a /etc/ssh/sshd_config
sudo service ssh restart
- Create a location to store repositories
sudo mkdir -p /home/repo
sudo chown -R git:www-data /home/repo
- Install gitweb
sudo apt-get install gitweb
- Update the gitweb configuration file at
/etc/gitweb.conf
sudo cp /etc/gitweb.conf /etc/gitweb.conf.$(date +%Y-%m-%d)
sudo sed -i 's/^\$projectroot.*/\$projectroot = \\"\\/home\\/repo\\";/' /etc/gitweb.conf
echo -e "\n\n# User additions" | sudo tee -a /etc/gitweb.conf
echo "\\$feature{'blame'}{'default'} = [1];" | sudo tee -a /etc/gitweb.conf
echo "\\$feature{'search'}{'default'} = [1];" | sudo tee -a /etc/gitweb.conf
echo "\\$feature{'highlight'}{'default'} = [1];" | sudo tee -a /etc/gitweb.conf
echo "\\$feature{'grep'}{'default'} = [1];" | sudo tee -a /etc/gitweb.conf
- Setup GitWeb executable in
/home/git
and use Kogakure's gitweb-theme
sudo mkdir /home/git/gitweb
sudo ln -s /usr/share/gitweb/gitweb.cgi /home/git/gitweb/index.cgi
git clone https://github.com/kogakure/gitweb-theme.git
sudo mv gitweb-theme /home/git/gitweb/static
sudo chown -R git:www-data /home/git/gitweb
- Edit Lighttpd configuration for localhost to include:
# GitWeb
$HTTP["url"] =~ "^/gitweb" {
server.document-root = "/home/git/"
server.indexfiles = ("index.cgi")
cgi.assign = (".cgi" => "")
}
Now that the infrastructure is setup, lets create a HelloWorld repository to test out the configuration.
- Create a bare repository
#Create the directory (always end with .git)
sudo mkdir /home/repo/helloworld.git
cd /home/repo/helloworld.git
#Initialize a bare repository
sudo git --bare init
#Some meta-data
echo "Hello World Repository. Testing system configuration" | sudo tee /home/repo/helloworld.git/description
echo "[gitweb]" | sudo tee -a /home/repo/helloworld.git/config
echo -e "\towner = \\"Rami Al-Ghanmi\\"" | sudo tee -a /home/repo/helloworld.git/config
#Fix ownership of repository
sudo chown -R git:www-data /home/repo/helloworld.git
- Clone the repository, though empty, and add some code. Ignore the warning about cloning an empty repository.
git clone git@$(hostname):/home/repo/helloworld.git
cd helloworld
wget https://raw.github.com/gist/3205222/HelloWorld.cpp
git add HelloWorld.cpp
git commit -m "Initial commit with HelloWorld in C++"
git push origin master
- Now that we know the repository works, lets check the web interface. The URL should be: http://localhost/gitweb and you should see the helloworld repository. To check the code-base, follow the tree link.
In conclusion, for any new repository you want to create follow the steps you did in Create a HelloWorld Repository.