Setup Git Server on macOS and Ubuntu

Installing Git

On macOS:

brew install git

On Ubuntu:

sudo apt-get install git

Add Git User

Add Git user (execute on Git server):

sudo adduser git

Setup SSH key

Generate ssh key (execute on local machine):

ssh-keygen -t rsa

Copy the public key to git server:

cat ~/.ssh/id_rsa.pub | ssh git@[SERVER_URL] "mkdir -p ~/.ssh && cat >>  ~/.ssh/authorized_keys"

Disable Shell for Git User (Optional)

Edit the user permission file /etc/passwd on Git server. Change git:x:1000:1000:,,,:/home/git:/bin/bash to git:x:1000:1000:,,,:/home/git:/usr/bin/git-shell

Test

Create a new repository on Git server:

git init --bare helloworld.git
sudo chown -R git:git helloworld.git

Clone the created repository from the Git server:

git clone git@[SERVER_URL]:/home/git/helloworld.git

Reference

Ubuntu搭建Git服务器

How to Run Your Own Git Server