Hosting your own git server can be a fun learning experience used to understand the ins and outs of maintaining a codebase in private environments. You can set up a working private git server in no time by following the guide if you have a Raspberry-Pi handy.
You can also configure on VM or AWS machines or any hosting service with Linux.
REMARK: This guide just shows the basic setup, security mechanisms regarding user privileges, and validation restrictions that still need to be configured but are beyond the scope of this article.
Let’s go! Let’s go….
STEP-1: Setting up a new user to manage Git repositories
For security and ease of use, I will configure SSH keys for b/w authentication of server and clients on the network.
a) Login to your existing pi user in raspberry-pi and setup a new user for your git server.
[email protected]:~ $ sudo useradd -s /bin/bash -m [username]
I will name my user
git and will refer to it for the rest of the article.
The above command adds a new user, -s designates the shell to use and -m indicates to create the home directory.
b) Give the password to your new user:
[email protected]:~ $ sudo passwd git
vs) Fill in the password of your choice and change the user to git and cd to the git home directory.
[email protected]:~ $ su git
D) Fill in the password.
[email protected]:home/pi $
and) and then type
cd to access the git home directory.
STEP-2: Configure SSH Key Authentication for Git
a) From your client computer, ssh into git to verify that you can remotely connect to the user.
[email protected]:~ $ ssh [email protected][ipaddr]
b) If you have set port 22 as default and the user is set to active, you will be prompted to enter the password and log in. Enter the credentials and you will be connected to the git user in your raspberry-pi.
vs) Open a new terminal tab and do the following on the client machine.
[email protected]:~ $ ssh-keygen
D) Follow the steps, give the key a unique name and add a password if you want.
e) Once the keys are configured, send your id pub key to the server
[email protected]:~ $ ssh-copy-id -i [location]/unique_key.pub [email protected][ipaddr]
F) Try logging in to the git user, if you are still prompted to log in with a password, then:
[email protected]:~ $ ssh-agent [your shell] && ssh-add [location]/unique_key
if you were logged in without a password, you can ignore the above command.
STEP-3: Configuring repositories
a) In your raspberry-pi connected as git, create a directory to store all git repositories:
[email protected]:~ $ mkdir [directory]
b) I named mine
srv. In srv create a directory for the project you want to manage ending with .git. This is because .git helps Linux differentiate between standard b/w directories and git-managed repositories.
[email protected]:~ $ cd srv g[email protected]:~/srv $ mkdir [nameofyourrepo].git
vs) I named mine
traceviz.git and will refer to it from now on.
Initialize the bare repository. Bare is needed because remote repositories need to track changes and not have working trees.
[email protected]:~/srv/traceviz.git $ git init --bare
a) Navigate to your project directory and initialize the git repository. Add a file and perform the initial commit.
[email protected]:~ $ cd Projects/traceroute_viz [email protected]:~/Projects/traceroute_viz $ git init [email protected]:~/Projects/traceroute_viz $ git add README.md [email protected]:~/Projects/traceroute_viz $ git commit -m 'README'
b) Add a remote address to your git repository.
[email protected]:~/Projects/traceroute_viz $ git remote add origin ssh://[email protected]/home/git/srv/traceviz.git
vs) Add your remote repository address after
D) Push to the remote server.
[email protected]:~/Projects/traceroute_viz $ git push -u ssh://[email protected]/home/git/srv/traceviz.git
This concludes setting up repositories to push code. 🙂
Test code pushed by cloning
a) On the client machine, navigate to a different directory than your project and clone the remote repository.
[email protected]:~/Downloads $ git clone [email protected]:/home/git/srv/traceroute_viz.git
And you will encounter this error:
b) To fix this, go to your remote machine and change
HEAD file on the git server.
vs) Original content from
HEAD to say:
D) And change master to hand. You can do this by simply editing files using your favorite editor (nano, vim, emacs or any other of your choice).
e) Delete your previous cloned repository and clone a new one and you will get all the files you need.