<title>Private Git repo · Sysadmining. All day. Every day.</title>
<metaname="description"content="I&rsquo;ve decided to migrate this blog to Pelican. I&rsquo;ve been playing around with it over the week-end, and it turns out to be way easier to manage than J"/>
<p>I’ve decided to migrate this blog to <ahref="http://blog.getpelican.com/">Pelican</a>. I’ve been playing around with it over the week-end, and it turns out to be way easier to manage than <ahref="https://jekyllrb.com/">Jekyll</a>. Themes are much easier to install and configure, so it ends up looking better as well !</p>
<p>Since I’m basically recreating this blog from scratch, I’ve decided to delete the old git repo that was hosting it and to create a new one.</p>
<p>Setting up your own private git repo is pretty easy to achieve and is already well-documented on the <ahref="https://git-scm.com/book/en/v2/Git-on-the-Server-Setting-Up-the-Server">Git</a> website.</p>
<p>Every time I want to create a new repo, I’ve had time to forget how to do it and I end up looking for that page, so I figured I’d write a few lines on the subject.</p>
<p>In this tutorial, I’ll configure a git repo on a distant server running Debian 8 (Jessie). This repo will be remotely accessible using SSH. Two users will be able to connect to it : me and the www-data user on my webserver.</p>
<h2id="ssh-keys">SSH Keys</h2>
<p>If you don’t have one already, you’ll need a ssh-key to connect to the git repo.</p>
<p>On your computer, in a shell, as your usual user :</p>
Enter file in which to save the key (/home/user/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_rsa.
Your public key has been saved in /home/user/id_rsa.pub.
The key fingerprint is:
[Redacted]
</code></pre>
<p>For security reasons, configuring a passphrase is recommended. On Mac OS X and most desktop environnements on Linux, you can store this passphrase for the duration of your session using the <code>ssh-add</code> command, so you won’t have to type it every time you want to connect to a host.</p>
<p>On the server, we also have to create a ssh-key for the user that is running our webserver (you’ll need to have sudo installed) :</p>
<p>This will create a system user (UID < 1000) with a /home/git home directory. If you want to host your git repos somewhere else on your filesystem, you should add a <code>-d /home/directory/for/git</code> in the previous command.</p>
<p>This user will use the git-shell shell. This limits remote connection to that user to git commands (like the rssh shell can limit remote connection to a user to scp or rsync commands).</p>
<p>We have to configure our system to allow the use of this shell :</p>
<p>You can now copy/paste the content of the two <code>$HOME/.ssh/id_rsa.pub</code> files we’ve created earlier using the <code>ssh-keygen</code> command in <code>/home/git/.ssh/authorized_keys</code>.</p>
<p>The last thing we have to do is to create our first git repo. In this example, my project will be called ‘captainarkdotnet’ as it will be hosting this blog :</p>
<p>The last command should give you the following output :</p>
<pre><codeclass="language-bash">Initialized empty Git repository in /home/git/captainarkdotnet.git/.git/
</code></pre>
<p>We’re done with the server configuration. Let’s now actually push stuff to our repo !</p>
<h3id="initial-push">Initial push</h3>
<p>The files for my blog are store in the ~/Documents/projects/captainarkdotnet on my computer. Before doing anything else, we first have to make sure that we currently are in that folder :</p>
<p>Please note that you’ll need to edit <strong>git.captainark.net</strong> to the FQDN or IP of your git server, and <strong>captainarkdotnet.git</strong> to the name of the git project on your server.</p>
<p>If everything went well, the last command should give you the following output :</p>
<p>That’s it, we’ve now pushed our first commit to our server !</p>
<h2id="first-pull">First pull</h2>
<p>Alright, time to pull the files we’ve just pushed on our webserver. I personally store my web content in <code>/var/www</code> ; if you don’t, you’ll have to adjust the path accordingly :</p>
<p>SSH will ask you to type ‘yes’ since it’s the first time the www-data user connects to the server. If everything goes well, you should have the following output :</p>
<pre><codeclass="language-bash">Cloning into 'captainarkdotnet'...
<p>That’s it ! We now have a working private git repo ! I won’t go into details into the git commands in this tutorial, but here’s a quick overwiew of the ones I use the most :</p>
<ul>
<li><code>git add .</code> recursively adds all files from the directory to the repo ;</li>
<li><code>git commit -a -m 'This is a comment'</code> commits the current state of your local repo with the ‘This is a comment’ comment ;</li>
<li><code>git push</code> pushes your commits to the distant repo ;</li>
<li><code>git pull</code> pulls the latest version of the distant repo locally ;</li>
<li><code>git branch -av</code> shows all available branches for the repo ;</li>
<li><code>git checkout -b testing remotes/origin/testing</code> create a local ‘testing’ branch based on the remote ‘remotes/origin/testing’ branch ;</li>
<li>once a branch has been copied locally, you can switch to it with the <code>git checkout {branch}</code> command.</li>
</ul>
<p>For more information on git a command, use <code>man git-{command}</code> !</p>
<p>If you’ve found this tutorial in any way helpful, please feel free to leave a comment !</p>
<sectionclass="poweredby">Proudly generated by <aclass="icon-hugo"href="http://gohugo.io">HUGO</a>, with <aclass="icon-theme"href="https://github.com/vjeantet/hugo-theme-casper">Casper</a> theme</section>