Self-Hosting Git Repositories

In-depth look at the commands that work

Previously, I shared why I need a solution away from the standard git providers, and how I setup a VPS to host git repositories. It will beneficial for some to know what these commands are doing so you can modify or debug them as needed. Here is a detailed look at the commands which were run.

Step-by-step

# SSH onto the remote host
$ ssh [USERNAME]@[REMOTE]

# Create a group to have ownership of the git directory
$ sudo groupadd [GROUPNAME]

# Add the a user to the group
$ sudo usermod --append --groups [GROUPNAME] [USERNAME]

# This is exactly what I ran.
# Referencing absolute directories may work as well.
# Moreover, it doesn't have to be in /opt.
$ cd /opt

# Make a directory to hold any number of git repositories
# Give group full rwx permissions, Give others no rwx permissions
$ sudo mkdir -m 0770 [DIRECTORYNAME]

# Change group ownership of the newly created directory to the new group
$ sudo chgrp [GROUPNAME] [DIRECTORYNAME]

# Configure the directory to have new files and directories inherit group ownership
$ sudo chmod g+s [DIRECTORYNAME]

# This is exactly what I ran.
$ cd [DIRECTORYNAME]

# Make a directory for a specific git repository
$ mkdir [PROJECTDIRECTORY]

# Change to the project directory to initialize git
$ cd [PROJECTDIRECTORY]

# git init --bare makes a repo without a working directory
# git init --shared configures git to run its automated processes maintaining group ownership
$ git init --bare --shared

# Exit back to the local desktop
$ exit

# Change path to the local git repository
$ cd [PROJECTPATH]

# Add the self-hosted git repo as a remote
$ git remote add [REMOTENAME] [USERNAME]@[REMOTE]:/opt/[DIRECTORYNAME]/[PROJECTDIRECTORY]

# After having added and committed files, push any changes to the remote repository
$ git push [REMOTENAME] HEAD

Self-Hosting Git Repositories was originally published in DevOps.dev on Medium, where people are continuing the conversation by highlighting and responding to this story.

alexa anderson

About the Author

Alexa Anderson is an engineer and evangelist with a penchant for making products geared for progress and achievement. When not writing software, Alexa spends her time curating content and sharing her discoveries.