git repo article / first version

This commit is contained in:
CaptainArk 2016-01-31 14:26:18 +01:00
parent 1bd901c391
commit 3be3a7927f
8 changed files with 142 additions and 50 deletions

View File

@ -1,16 +1,16 @@
Title: Private git repo
Title: Private Git Repo
Date: 2016-01-31
Category: Tutorial
## Introduction
I've decided to migrate this blog to [Pelican](http://blog.getpelican.com/). I've been playing around with it over the week-end, and it turns out to be way easier to manage than [Jekyll](https://jekyllrb.com/). Themes are way easier to install and configure, so it ends up looking better as well !
I've decided to migrate this blog to [Pelican](http://blog.getpelican.com/). I've been playing around with it over the week-end, and it turns out to be way easier to manage than [Jekyll](https://jekyllrb.com/). Themes are much easier to install and configure, so it ends up looking better as well !
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.
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.
Setting up your own private git repo is pretty easy to achieve and is already well-documented on the [Git](https://git-scm.com/book/en/v2/Git-on-the-Server-Setting-Up-the-Server) website.
However, since every time I want to create a new repo , I end up have to look for that page since I've had time to forget how to do it, I figured I'd write a few lines on the subject.
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.
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.
@ -32,9 +32,9 @@ The key fingerprint is:
[Redacted]
```
For security reasons, configuring a passphrase is recommanded. On Mac OS X and most desktop environnements on Linux, you can store this passphrase for the duration of your session using the `ssh-add` command.
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 `ssh-add` command, so you won't have to type it every time you want to connect to a host.
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 on your server) :
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) :
```bash
sudo -H -u www-data ssh-keygen -t rsa -b 3072
@ -52,13 +52,15 @@ If you decide to configure a passphrase for that ssh-key, you'll have to type it
## Server management
First thing first, we have to install the git package on the server that will be hosting the git repo :
All of the commands in this section have to be run as root.
First thing first, we have to install the git package on the server that will be hosting our git repos :
```bash
apt update && apt install git -y
```
Then, we will create a user named git :
Then, we have to create a user named git :
```bash
useradd -s /usr/bin/git-shell -m -r git
@ -66,7 +68,7 @@ useradd -s /usr/bin/git-shell -m -r git
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 `-d /home/directory/for/git` in the previous command.
This user will also 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).
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).
We have to configure our system to allow the use of this shell :
@ -74,6 +76,15 @@ We have to configure our system to allow the use of this shell :
echo '/usr/bin/git-shell' >> /etc/shells
```
From this point, you should have to following output if you try to SSH to your server with that user :
```bash
ssh git@git.captainark.net
fatal: Interactive git shell is not enabled.
hint: ~/git-shell-commands should exist and have read and execute access.
Connection to git@git.captainark.net closed.
```
We now need to create the .ssh/authorized_keys file for the git user with the correct permissions :
```bash
@ -81,30 +92,30 @@ sudo -H -u git mkdir /home/git/.ssh && chmod 700 /home/git/.ssh
sudo -H -u git touch /home/git/.ssh/authorized_keys && chmod 600 /home/git/.ssh/authorized_keys
```
You can now copy the content of the two $HOME/.ssh/id_rsa.pub files we've created earlier using the `ssh-keygen` command in /home/git/.ssh/authorized_keys.
You can now copy/paste the content of the two `$HOME/.ssh/id_rsa.pub` files we've created earlier using the `ssh-keygen` command in `/home/git/.ssh/authorized_keys`.
The last thing we have to do is to create our first git repo. In this example, my project will be called 'captainarknet' as it will be hosting this blog :
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 :
```bash
sudo -H -u git mkdir /home/git/captainarknet.git
cd /home/git/captainarknet.git
sudo -H -u git mkdir /home/git/captainarkdotnet.git
cd /home/git/captainarkdotnet.git
sudo -H -u git git init --bare
```
The last command should give you the following output :
```bash
Initialized empty Git repository in /home/git/captainarknet.git/.git/
Initialized empty Git repository in /home/git/captainarkdotnet.git/.git/
```
We're done with the server configuration. Let's now actually push stuff to our repo !
### Initial push
The files for my blog are store in the ~/Documents/projects/captainarknet on my computer. Before doing anything else, we first have to make sure that we currently are in that folder :
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 :
```bash
cd ~/Documents/projects/captainarknet
cd ~/Documents/projects/captainarkdotnet
```
Let's now push the content of that folder to our repo :
@ -113,11 +124,11 @@ Let's now push the content of that folder to our repo :
git init
git add .
git commit -m 'initial commit'
git remote add origin git@git.captainark.net:captainarknet.git
git remote add origin git@git.captainark.net:captainarkdotnet.git
git push origin master
```
Please note that you'll need to edit **git.captainark.net** to the FQDN or IP of your git server, and **captainarknet.git** to the name of the git project on your server.
Please note that you'll need to edit **git.captainark.net** to the FQDN or IP of your git server, and **captainarkdotnet.git** to the name of the git project on your server.
If everything went well, the last command should give you the following output :
@ -127,19 +138,45 @@ Delta compression using up to 4 threads.
Compressing objects: 100% (64/64), done.
Writing objects: 100% (69/69), 1.01 MiB | 0 bytes/s, done.
Total 69 (delta 15), reused 0 (delta 0)
To git@git.captainark.net:captainarknet.git
To git@git.captainark.net:captainarkdotnet.git
* [new branch] master -> master
```
Thats'it, we've now pushed our first commit to our server !
That's it, we've now pushed our first commit to our server !
## First pull
Alright, time to pull the files we've just pushed on our webserver. I personally store my web content in /var/www ; if you don't, you'll have to adjust the path accordingly :
Alright, time to pull the files we've just pushed on our webserver. I personally store my web content in `/var/www` ; if you don't, you'll have to adjust the path accordingly :
```bash
cd /var/www
sudo -H -u www-data git pull git@git.captainark.net:captainarknet.git
sudo -H -u www-data git clone git@git.captainark.net:captainarkdotnet.git
```
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 :
```bash
Cloning into 'captainarkdotnet'...
remote: Counting objects: 70, done.
remote: Compressing objects: 100% (65/65), done.
remote: Total 70 (delta 16), reused 0 (delta 0)
Receiving objects: 100% (70/70), 1.01 MiB | 0 bytes/s, done.
Resolving deltas: 100% (16/16), done.
Checking connectivity... done.
```
## Conclusion
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 :
- `git add .` recursively adds all files from the directory to the repo ;
- `git commit -a -m 'This is a comment'` commits the current state of your local repo with the 'This is a comment' comment ;
- `git push` pushes your commits to the distant repo ;
- `git pull` pulls the latest version of the distant repo locally ;
- `git branch -av` shows all available branches for the repo ;
- `git checkout -b testing remotes/origin/testing` create a local 'testing' branch based on the remote 'remotes/origin/testing' branch ;
- once a branch has been copied locally, you can switch to it with the `git checkout {branch}` command.
For more information on git a command, use `man git-{command}` !
If you've found this tutorial in any way helpful, please feel free to leave a comment !

Binary file not shown.

Before

Width:  |  Height:  |  Size: 77 B

After

Width:  |  Height:  |  Size: 77 B

View File

@ -88,7 +88,7 @@
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<dl>
<dt>Sun 31 January 2016</dt>
<dd><a href="./private-git-repo.html">Private git repo</a></dd>
<dd><a href="./private-git-repo.html">Private Git Repo</a></dd>
<dt>Tue 05 May 2015</dt>
<dd><a href="./flexget-init-script.html">Flexget init script</a></dd>
<dt>Fri 24 April 2015</dt>

View File

@ -87,9 +87,9 @@
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<div class="post-preview">
<a href="../private-git-repo.html" rel="bookmark" title="Permalink to Private git repo">
<a href="../private-git-repo.html" rel="bookmark" title="Permalink to Private Git Repo">
<h2 class="post-title">
Private git repo
Private Git Repo
</h2>
</a>
<h2>Introduction</h2>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 77 B

After

Width:  |  Height:  |  Size: 77 B

View File

@ -87,9 +87,9 @@
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<div class="post-preview">
<a href="../private-git-repo.html" rel="bookmark" title="Permalink to Private git repo">
<a href="../private-git-repo.html" rel="bookmark" title="Permalink to Private Git Repo">
<h2 class="post-title">
Private git repo
Private Git Repo
</h2>
</a>
<h2>Introduction</h2>

View File

@ -90,9 +90,9 @@
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<div class="post-preview">
<a href="./private-git-repo.html" rel="bookmark" title="Permalink to Private git repo">
<a href="./private-git-repo.html" rel="bookmark" title="Permalink to Private Git Repo">
<h2 class="post-title">
Private git repo
Private Git Repo
</h2>
</a>
<h2>Introduction</h2>

View File

@ -9,7 +9,7 @@
<meta name="author" content="">
<title>Private git repo</title>
<title>Private Git Repo</title>
<!-- Bootstrap Core CSS -->
@ -45,7 +45,7 @@
<meta property="og:type" content="article">
<meta property="article:author" content="">
<meta property="og:url" content="./private-git-repo.html">
<meta property="og:title" content="Private git repo">
<meta property="og:title" content="Private Git Repo">
<meta property="og:description" content="">
<meta property="og:image" content="./">
<meta property="article:published_time" content="2016-01-31 00:00:00+01:00">
@ -88,7 +88,7 @@
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<div class="post-heading">
<h1>Private git repo</h1>
<h1>Private Git Repo</h1>
<span class="meta">Posted by
<a href="./author/antoine-joubert.html">Antoine Joubert</a>
on Sun 31 January 2016
@ -107,12 +107,12 @@
<!-- Post Content -->
<article>
<h2>Introduction</h2>
<p>I've decided to migrate this blog to <a href="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 <a href="https://jekyllrb.com/">Jekyll</a>. Themes are way 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>I've decided to migrate this blog to <a href="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 <a href="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 <a href="https://git-scm.com/book/en/v2/Git-on-the-Server-Setting-Up-the-Server">Git</a> website.</p>
<p>However, since every time I want to create a new repo , I end up have to look for that page since I've had time to forget how to do it, I figured I'd write a few lines on the subject.</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>
<h2>SSH keys</h2>
<h2>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>
<div class="highlight"><pre>ssh-keygen -t rsa -b 3072
@ -127,8 +127,8 @@ The key fingerprint is:
</pre></div>
<p>For security reasons, configuring a passphrase is recommanded. 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.</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 on your server) :</p>
<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>
<div class="highlight"><pre>sudo -H -u www-data ssh-keygen -t rsa -b 3072
Generating public/private rsa key pair.
Enter file in which to save the key <span class="o">(</span>/var/www/.ssh/id_rsa<span class="o">)</span>:
@ -143,56 +143,111 @@ The key fingerprint is:
<p>If you decide to configure a passphrase for that ssh-key, you'll have to type it every time you'll want to pull from your repo.</p>
<h2>Server management</h2>
<p>First thing first, we have to install the git package on the server that will be hosting the git repo :</p>
<p>All of the commands in this section have to be run as root.</p>
<p>First thing first, we have to install the git package on the server that will be hosting our git repos :</p>
<div class="highlight"><pre>apt update <span class="o">&amp;&amp;</span> apt install git -y
</pre></div>
<p>Then, we will create a user named git :</p>
<p>Then, we have to create a user named git :</p>
<div class="highlight"><pre>useradd -s /usr/bin/git-shell -m -r git
</pre></div>
<p>This will create a system user (UID &lt; 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 also 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>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>
<div class="highlight"><pre><span class="nb">echo</span> <span class="s1">&#39;/usr/bin/git-shell&#39;</span> &gt;&gt; /etc/shells
</pre></div>
<p>From this point, you should have to following output if you try to SSH to your server with that user :</p>
<div class="highlight"><pre>ssh git@git.captainark.net
fatal: Interactive git shell is not enabled.
hint: ~/git-shell-commands should exist and have <span class="nb">read</span> and execute access.
Connection to git@git.captainark.net closed.
</pre></div>
<p>We now need to create the .ssh/authorized_keys file for the git user with the correct permissions :</p>
<div class="highlight"><pre>sudo -H -u git mkdir /home/git/.ssh <span class="o">&amp;&amp;</span> chmod <span class="m">700</span> /home/git/.ssh
sudo -H -u git touch /home/git/.ssh/authorized_keys <span class="o">&amp;&amp;</span> chmod <span class="m">600</span> /home/git/.ssh/authorized_keys
</pre></div>
<p>You can now copy the content of the two $HOME/.ssh/id_rsa.pub files we've created earlier using the <code>ssh-keygen</code> command in /home/git/.ssh/authorized_keys.</p>
<p>The last thing we have to do is to create our first git repo. In this example, my project will be called 'captainarknet' as it will be hosting this blog :</p>
<div class="highlight"><pre>sudo -H -u git mkdir /home/git/captainarknet.git
<span class="nb">cd</span> /home/git/captainarknet.git
<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>
<div class="highlight"><pre>sudo -H -u git mkdir /home/git/captainarkdotnet.git
<span class="nb">cd</span> /home/git/captainarkdotnet.git
sudo -H -u git git init --bare
</pre></div>
<p>The last command should give you the following output :</p>
<div class="highlight"><pre>Initialized empty Git repository in /home/git/captainarknet.git/.git/
<div class="highlight"><pre>Initialized empty Git repository in /home/git/captainarkdotnet.git/.git/
</pre></div>
<p>We're done with the server configuration. Let's now actually push stuff to our repo !</p>
<h3>Initial push</h3>
<p>The files for my blog are store in the ~/Documents/projects/captainarknet on my computer. Before doing anything else, we first have to make sure that we currently are in that folder :</p>
<div class="highlight"><pre><span class="nb">cd</span> ~/Documents/projects/captainarknet
<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>
<div class="highlight"><pre><span class="nb">cd</span> ~/Documents/projects/captainarkdotnet
</pre></div>
<p>Let's now push the content of that folder to our repo :</p>
<div class="highlight"><pre>git init
git add .
git commit -m &#39;initial commit&#39;
git remote add origin git@git.captainark.net:captainarknet.git
git commit -m <span class="s1">&#39;initial commit&#39;</span>
git remote add origin git@git.captainark.net:captainarkdotnet.git
git push origin master
</pre></div>
<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>
<div class="highlight"><pre>Counting objects: 69, <span class="k">done</span>.
Delta compression using up to <span class="m">4</span> threads.
Compressing objects: 100% <span class="o">(</span>64/64<span class="o">)</span>, <span class="k">done</span>.
Writing objects: 100% <span class="o">(</span>69/69<span class="o">)</span>, 1.01 MiB <span class="p">|</span> <span class="m">0</span> bytes/s, <span class="k">done</span>.
Total <span class="m">69</span> <span class="o">(</span>delta 15<span class="o">)</span>, reused <span class="m">0</span> <span class="o">(</span>delta 0<span class="o">)</span>
To git@git.captainark.net:captainarkdotnet.git
* <span class="o">[</span>new branch<span class="o">]</span> master -&gt; master
</pre></div>
<p>That's it, we've now pushed our first commit to our server !</p>
<h2>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>
<div class="highlight"><pre><span class="nb">cd</span> /var/www
sudo -H -u www-data git clone git@git.captainark.net:captainarkdotnet.git
</pre></div>
<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>
<div class="highlight"><pre>Cloning into <span class="s1">&#39;captainarkdotnet&#39;</span>...
remote: Counting objects: 70, <span class="k">done</span>.
remote: Compressing objects: 100% <span class="o">(</span>65/65<span class="o">)</span>, <span class="k">done</span>.
remote: Total <span class="m">70</span> <span class="o">(</span>delta 16<span class="o">)</span>, reused <span class="m">0</span> <span class="o">(</span>delta 0<span class="o">)</span>
Receiving objects: 100% <span class="o">(</span>70/70<span class="o">)</span>, 1.01 MiB <span class="p">|</span> <span class="m">0</span> bytes/s, <span class="k">done</span>.
Resolving deltas: 100% <span class="o">(</span>16/16<span class="o">)</span>, <span class="k">done</span>.
Checking connectivity... <span class="k">done</span>.
</pre></div>
<h2>Conclusion</h2>
<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>
</article>