The Filesystem Hierarchy Standard (FHS) doesn't explicitly define a standard location for Git repositories, leading to various common practices in the wild.
Your suggestion of /var/git
makes logical sense because:
/var
is designated for variable data (like version-controlled content)- It follows the same pattern as
/var/www
for web content - System services can easily access repositories here
Example setup commands:
sudo mkdir -p /var/git
sudo chown git:git /var/git
sudo chmod 2775 /var/git
In practice, you'll also see:
/srv/git
- For service-related data (FHS recommendation)/home/git
- When using dedicated git user/opt/git
- For self-contained applications
GitLab CE defaults to /var/opt/gitlab/git-data
showing how larger systems implement this:
# In gitlab.rb configuration
git_data_dirs({
"default" => {
"path" => "/var/opt/gitlab/git-data"
}
})
Regardless of location, proper permissions are crucial:
# Typical permission setup
sudo groupadd git
sudo useradd -g git git
sudo chown -R git:git /var/git
sudo find /var/git -type d -exec chmod 2770 {} \;
For Git servers, here's a sample unit file:
[Unit]
Description=Git Server
After=network.target
[Service]
User=git
Group=git
ExecStart=/usr/bin/git daemon \
--base-path=/var/git \
--export-all \
--reuseaddr \
--verbose
Restart=always
[Install]
WantedBy=multi-user.target
For most cases, I recommend:
- Single-user systems:
~/git/
- Multi-user servers:
/srv/git/
- Enterprise services:
/var/lib/git/
The key is consistency - document your choice and stick with it across your infrastructure.
In Linux systems, the Filesystem Hierarchy Standard (FHS) defines where different types of files should be stored. For Git repositories, there are several conventional locations:
/srv/git/ # Primary location for service data (recommended)
/var/git/ # Alternative location (less common)
/home/git/ # Common for user-managed repositories
/opt/git/ # Occasionally used for self-contained installations
The most technically correct location would be /srv/git/
according to FHS:
sudo mkdir -p /srv/git
sudo chown git:git /srv/git
sudo chmod 750 /srv/git
This follows the FHS guideline that /srv/
contains:
- Data for services provided by the system
- Site-specific data
- Service-specific data (git in this case)
Here's how to properly set up a bare repository in /srv/git/
:
# Create the repository
sudo -u git mkdir /srv/git/myrepo.git
cd /srv/git/myrepo.git
sudo -u git git init --bare
# Set proper permissions
sudo chown -R git:git /srv/git/myrepo.git
sudo chmod -R 750 /srv/git/myrepo.git
# Clone from another machine
git clone git@yourserver:/srv/git/myrepo.git
While /var/git/
might seem logical (similar to /var/www/
), there are technical differences:
/var/git/ | /srv/git/ |
---|---|
Traditionally for variable data files | Specifically for service data |
Often used for transient data | Best for persistent service data |
Might get cleaned by system | Protected location for service data |
Many organizations use /home/git/
for simplicity:
sudo adduser git
sudo -u git mkdir /home/git/repositories
cd /home/git/repositories
sudo -u git git init --bare project.git
This approach works well for:
- Smaller deployments
- Personal development servers
- Systems where
/srv/
isn't available
Regardless of location, proper permissions are crucial:
# Recommended permission structure
sudo chown -R git:developers /srv/git
sudo chmod -R 2770 /srv/git # Setgid preserves group ownership
# For web-based interfaces like GitWeb:
sudo usermod -a -G developers www-data
When using Git servers, the location affects configuration:
# GitLab example in /etc/gitlab/gitlab.rb
git_data_dirs({
"default" => {
"path" => "/srv/git"
}
})
# Gitolite configuration
$REPO_BASE = "/srv/git";