July 26, 2017

How to Setup a Git Repository on FreeBSD

Git Logo

The open source distributed version control and source code management system Git is designed to handle projects with speed and efficiency. Git is not dependent on network access or a central server. Every Git working directory is a full-fledged repository with complete history and full version tracking capabilities. Git is free distributed under the terms of the GNU General Public License version 2. In this tutorial you will learn how to setup a Git repository on FreeBSD. SSH will be used for commits.

Root access is required to edit the following files and to execute commands. Log in as root (su) or simply prepend sudo to all commands that require root privileges.

Install git

Install devel/git from the ports tree. Optinally select GITWEB. SVN, P4 or CVS.

cd /usr/ports/devel/git
make install clean; rehash

Create a git user

Create a git user with the uid and gid 9418

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 -

Create the git directory

For the git user we use the git-shell. The home directory is set as /git/. The repositories will be saved under /git/base/.

chown git:git /git/
chmod 755 /git
mkdir /git/base/
chown git:git /git/base/
chmod 775 /git/base/

Create SSH keys

SSH keys are used for authenication. Collect the public keys of all the users who need commit access.

mkdir /git/.ssh/
chmod 700 /git/.ssh/
touch /git/.ssh/authorized_keys
chmod 600 /git/.ssh/authorized_keys

Add the public keys to the authorized_keys file and change the user and group access of the /git/.ssh/ directory to git.

chown -R git:git /git/.ssh/

Modify /etc/rc.conf

Make these settings in you /etc/rc.conf to load Git on system-start:

git_daemon_enable="YES"
git_daemon_directory="/git"
git_daemon_flags="--syslog --base-path=/git --export-all --reuseaddr --detach"

Start git

Start the git daemon with:

/usr/local/etc/rc.d/git_daemon start

Test the git installation

Finally, let’s create a repository for testing. Change to a user that has been added to the git group.

mkdir /git/base/test.git
cd /git/base/test.git && git init --bare --shared

Create a local repository and commit:

mkdir ~/test
cd ~/test && git init
echo 'test' > test
git add .
git commit -m 'test commit'

Push the commit it into the remote repository. Replace s1.domain.com with your own hostname.

git remote add origin git@s1.domain.com:base/test.git
git push origin master

Now you can clone the repository on your local machine. Replace s1.domain.com with your own hostname.

git clone git://s1.domain.com/base/test.git

Comments

  1. EKO SUHARTONO says:

    Thank you about your article havebeen wrote. Btw, it’s very help me to write article about freeBSD too and ‘ll go publish to my website on http://courseplus.cf/

Speak Your Mind