Hosting your own git server can be a fun learning experience used to understand the ins and outs of what goes into maintaining a codebase in private environments. You can set up a working private git-server in no time following the guide if you have a Raspberry-Pi handy.
You can also set up on VM or AWS machines or any hosting service with Linux.
NOTE: This guide just shows the basic setup, security mechanisms regarding user privileges, and committing restrictions that still need to be set up but are beyond the scope of this article.
For security purposes and ease of use, I’ll be setting up SSH keys for authentication b/w server and clients over the network.
a) Log in to your existing pi user in raspberry-pi and set up a new user for your git-server.
[email protected]:~ $ sudo useradd -s /bin/bash -m [username]
I’ll name my user
git and will be referring to it for the rest of the article.
The above command adds a new user, -s denotes shell to be used and -m denotes to create the home directory.
b) Give the password to your new user:
[email protected]:~ $ sudo passwd git
c) Fill in the password of your choice and change user to git and cd into git’s home directory.
[email protected]:~ $ su git
d) Fill in the password.
[email protected]:home/pi $
e) And then type
cd to go to git’s home directory.
a) From your client machine, ssh into git to verify that you can remotely login into the user.
[email protected]:~ $ ssh [email protected][ipaddr]
b) If you have default port 22 set and user is set to active, you’ll be prompted for the password and log in. Enter the creds and you’ll be logged into git user in your raspberry-pi.
c) Open new terminal tab, and do the following on the client machine.
[email protected]:~ $ ssh-keygen
d) Follow the steps, give a unique name to key and add a password if you wish.
e) After the keys have been set up, send your identification pub key to the server
[email protected]:~ $ ssh-copy-id -i [location]/unique_key.pub [email protected][ipaddr]
f) Try logging into git user, if you’re 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 passwordless, then you can ignore the above command.
a) Within your raspberry-pi logged in as git, make a directory to store all git repositories:
[email protected]:~ $ mkdir [directory]
b) I Named mine
srv. Within srv, create a directory for the project you want to manage ending with .git. This is because .git helps Linux differentiate b/w standard directories and git-managed repositories.
[email protected]:~ $ cd srv [email protected]:~/srv $ mkdir [nameofyourrepo].git
c) I Named mine
traceviz.git and will be referring to it from now on.
Initialize bare repository. Bare is needed because remote repositories need to keep track of changes and not have working trees.
[email protected]:~/srv/traceviz.git $ git init --bare
a) Go to your project directory and initialize git repository. Add a file and do 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
c) Add the address of your remote repository after
d) Push to the remote server.
[email protected]:~/Projects/traceroute_viz $ git push -u ssh://[email protected]/home/git/srv/traceviz.git
That concludes setting up repositories for pushing code. :)
a) On the client machine, go 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’ll encounter this error:
b) To fix it, go to your remote machine and modify
HEAD file in the git server.
c) Original contents of
d) And change master to main. You can do so by simply editing files using your favorite editor (nano, vim, emacs, or any other of your choice).
e) Delete your earlier cloned repo, and clone a new one and you’ll get all the files you need.