raspberry+git_将Raspberry Pi设置为Git服务器

raspberry+git

In a recent article, Jérémy introduced the Raspberry Pi, a tiny computer about the size of your hand. In this article, he gives another example of the impressive uses of this amazing device.

杰里米(Jérémy)在最近的一篇文章中介绍了Raspberry Pi ,这是一种与您的手大小差不多的微型计算机。 在本文中,他给出了此惊人设备令人印象深刻的用法的另一个示例。

Git to Raspberry Pi

Managing versions of a project is useful. Even if your project is private, it’s always a good thing to be able to retrieve an old version, if (for instance) the new one is broken for some reason.

管理项目的版本很有用。 即使您的项目是私有的,如果(例如)新版本由于某种原因被破坏,能够检索旧版本始终是一件好事。

To manage versions of a project, we can create a new folder for each new version but, today, we have great tools that allow us to do far better.

要管理项目的版本,我们可以为每个新版本创建一个新文件夹,但是如今,我们拥有出色的工具,可以使我们做得更好。

One of these tools is Git. Git is great for managing versions of a project, whether you’re alone working on this project or not. This tool can be used on a single computer without any problem. However, if you want to share your code between several computers, you’ll need a Git server. That’s what we’ll build in this tutorial.

这些工具之一是Git 。 无论您是否独自从事此项目,Git都非常适合管理项目的版本。 此工具可以在单台计算机上使用,没有任何问题。 但是,如果要在多台计算机之间共享代码,则需要一个Git服务器。 这就是我们将在本教程中构建的内容。

First I’ll explain what we’ll build, and why we need it. Then, we’ll see concretely how to build our server with a Raspberry Pi. Finally, we’ll see how to use our server.

首先,我将解释我们将要构建的内容以及为什么需要它。 然后,我们将具体看到如何使用Raspberry Pi构建服务器。 最后,我们将看到如何使用我们的服务器。

我们想做什么? (What Do We Want to Do?)

Git服务器? (A Git server?)

As said above, you can use Git on a single computer, just to manage versions of a private project. The problems come when you want to share your code, either publicly or between two or more of your computers.

如上所述,您可以在单台计算机上使用Git,仅用于管理私有项目的版本。 当您要公开共享代码或在两台或更多台计算机之间共享代码时,就会出现问题。

In this case, you need a place to send your modifications, so they can be retrieved on the other computers. This place is a server.

在这种情况下,您需要一个位置来发送您的修改,以便可以在其他计算机上检索它们。 这个地方是一台服务器。

With this server, the process will be as follows:

使用此服务器,过程将如下所示:

  1. you modify your code

    您修改代码
  2. you send your changes to the server

    您将更改发送到服务器
  3. on another computer, you or anybody else downloads the changes from the server

    在另一台计算机上,您或其他任何人都从服务器下载更改
  4. new changes are made from this other computer

    从另一台计算机进行了新更改
  5. these changes are sent to the server so that others can download them, and so on.

    这些更改将发送到服务器,以便其他人可以下载它们,依此类推。

A Git server is basically a computer connected to the Internet on which we can store Git repositories. Any computer can be used for that and, here, we’ll use a Raspberry Pi, as it has the advantage of being a very low cost option, while giving good performance.

Git服务器基本上是一台连接到Internet的计算机,我们可以在该计算机上存储Git存储库。 任何计算机都可以用于此目的,在这里,我们将使用Raspberry Pi ,因为它的优点是价格非常低廉,同时具有良好的性能。

为什么要创建我们自己的服务器? (Why create our own server?)

There are various services around the Web that allow us to share Git repositories—like GitHub, which is one of the most popular.

Web上有多种服务可以共享Git存储库,例如GitHub,这是最受欢迎的服务之一。

The problem is that repositories on services like these are often public. Sometimes, we don’t want to share our code publicly, but rather to share it with just a few people (or even just another computer we own).

问题在于此类服务的存储库通常是公共的。 有时,我们不想公开共享我们的代码,而只与几个人(甚至只是我们拥有的另一台计算机)共享代码。

Some services like GitHub offer private repositories — so that you can choose who sees your code — though they’re not free. That’s why creating our own server can be a good idea.

诸如GitHub之类的某些服务提供了私有存储库-这样您就可以选择谁可以看到您的代码-尽管它们不是免费的。 这就是为什么创建我们自己的服务器是一个好主意的原因。

我们不在这里重新创建GitHub (We’re not recreating GitHub here)

Just to be clear, though: we’re not attempting to replicate GitHub with a simple Raspberry Pi. The server we’ll create is a very simple one, with all the features Git has, but no more. So there won’t be the facility for things like pull requests, or even a graphical interface.

不过,要清楚一点:我们不尝试使用简单的Raspberry Pi复制GitHub。 我们将创建的服务器非常简单,具有Git的所有功能,但仅此而已。 因此,不会提供诸如拉取请求甚至图形界面之类的功能。

Even if simple, though, this server will be far from useless. I currently have a Git server on my Raspberry Pi. I use it to share code between my desktop and my laptop. I use GitHub for some projects, but not for all of them. For some private projects, or for experimental things I don’t want to share for now, I use my little server. That way, I can work from any computer without having to publicly share my code.

即使简单,该服务器也绝不会无用。 我目前在Raspberry Pi上有一个Git服务器。 我使用它在台式机和笔记本电脑之间共享代码。 我将GitHub用于某些项目,但不用于所有项目。 对于一些私人项目,或者我暂时不想共享的实验性产品,我使用我的小服务器。 这样,我可以在任何计算机上工作而不必公开共享我的代码。

我们需要什么? (What will we need?)

There are several versions of the Raspberry Pi, from the very small and cheap Raspberry Pi Zero to the recent and powerful Raspberry Pi 3. Which should you choose for your Git server?

Raspberry Pi有多种版本,从非常小巧且便宜的Raspberry Pi Zero到最新且功能强大的Raspberry Pi3。您应该为Git服务器选择哪个?

I didn’t test on a Raspberry Pi Zero, so, to be honest, I won’t be able to say if this one is sufficient (I think it is, though). However, the very first version of the Raspberry Pi is largely enough and, as the Pi 2 and 3 are available, maybe you can find the first Raspberry Pi for a small price, so it’s a good option to think about.

我没有在Raspberry Pi Zero上进行测试,因此,老实说,我无法说出这个是否足够(不过我认为是足够的)。 但是,Raspberry Pi的第一个版本就足够了,并且由于提供了Pi 2和3,也许您可​​以以较低的价格找到第一个Raspberry Pi,所以这是一个不错的选择。

Anyway, the Raspberry Pi is just a motherboard. Whatever version you choose, you will need at least a case to protect it, a power source (via microUSB, like a smartphone), an SD card to store the OS (a classical SD card or a microSD following the version of the Pi you chose) and a way to connect the Pi to the Internet. Depending on the Pi you have, you can connect it through Ethernet, directly via Wi-Fi (for the Pi 3) or with a Wi-Fi key.

无论如何,Raspberry Pi只是主板。 无论选择哪种版本,您都将至少需要一个保护套,电源(通过microUSB,例如智能手机),用于存储OS的SD卡(传统的SD卡或您所用Pi版本之后的microSD)选择)以及将Pi连接到Internet的方法。 根据您拥有的Pi,您可以通过以太网,直接通过Wi-Fi(对于Pi 3)或使用Wi-Fi密钥将其连接。

Now that you have everything you need for this project, let’s see how to get a Git server from this small computer!

现在您已经拥有了该项目所需的一切,让我们看看如何从这台小型计算机上获取Git服务器!

如何使用Raspberry Pi构建Git服务器 (How To Build a Git Server with a Raspberry Pi)

安装Git (Installing Git)

The first thing our server will need is Git. You can find it in the git package, which you can install by executing the following command on the Raspberry Pi:

我们的服务器需要的第一件事是Git。 您可以在git包中找到它,可以通过在Raspberry Pi上执行以下命令来安装它:

sudo apt-get install git

From a very basic view, that’s all we need to get a Git server. However, there are some details we can add to get a proper and more secure server.

从最基本的角度来看,这就是获得Git服务器所需要的。 但是,我们可以添加一些细节以获取适当且更安全的服务器。

创建一个专用用户 (Creating a dedicated user)

It’s a good practice to create a new user for each use you make of the Raspberry Pi. This user is the official manager of its corresponding use.

每次使用Raspberry Pi时,创建一个新用户都是一个好习惯。 该用户是其相应用途的正式管理员。

Some programs will automatically adds their own users. For instance, Kodi, the media center, creates the user kodi. In the same vein, installing a LAMP solution (for getting a web server) will create the user www-data. Here we’ll create the user git. To do that, type the following command on the Raspberry Pi:

有些程序会自动添加自己的用户。 例如,媒体中心Kodi创建用户kodi 。 同样,安装LAMP解决方案(用于获取Web服务器)将创建用户www-data 。 在这里,我们将创建用户git 。 为此,请在Raspberry Pi上键入以下命令:

sudo adduser git

If you want to, you can change the name of the dedicated user to another one, by changing git in the above command.

如果需要,可以通过在上述命令中更改git来将专用用户的名称更改为另一个。

You’ll be asked to enter a password for this user. Then, you’ll be able to enter some other information like the real name of the user or their phone number. As git is not a real person, you can skip all these questions without any problem.

系统会要求您输入该用户的密码。 然后,您将能够输入其他信息,例如用户的真实姓名或电话号码。 由于git不是一个真实的人,因此您可以毫无问题地跳过所有这些问题。

使用SSH访问服务器 (Using SSH to access the server)

Having a dedicated screen for a server is pretty much useless. Most of the time, we just want to exchange data with it. To do that we can use SSH, a secure way to communicate between two computers.

为服务器配备专用屏幕几乎没有用。 大多数时候,我们只想与之交换数据。 为此,我们可以使用SSH(一种在两台计算机之间进行通信的安全方式)。

Explaining how SSH works is not the aim of this article, so I won’t describe how to use it. However, you can find excellent documentation on Raspberry Pi’s official website.

解释SSH的工作方式不是本文的目的,因此我将不介绍如何使用它。 但是,您可以在Raspberry Pi的官方网站上找到出色的文档

如何使用我们的服务器 (How To Use Our Server)

Our server is now ready to use. So it’s time to see how to use it. We’ll see here how to create a new repository, and how to update it.

我们的服务器现在可以使用了。 因此,现在该看看如何使用它了。 我们将在这里看到如何创建新的存储库以及如何对其进行更新。

Let’s show our great powers of imagination to the world by creating a test repository named Hello-World.

通过创建一个名为Hello-World的测试存储库,让我们向世界展示我们强大的想象力。

在Raspberry Pi上创建一个新的仓库 (Creating a new repo on the Raspberry Pi)

The first thing to do is initialize our new repository on the Raspberry Pi. On the server side, a repository is basically the same thing you find on your computer: a folder with a .git subfolder. So the first thing to do is to create this folder.

首先要做的是在Raspberry Pi上初始化我们的新存储库。 在服务器端,存储库基本上与您在计算机上找到的存储库相同:带有.git子文件夹的文件夹。 因此,第一件事就是创建此文件夹。

On the Raspberry Pi, use the git user to create it (through SSH or not, but you should always use this user to create new repositories). You can create this folder anywhere you want: on the Raspberry Pi’s SD card, for instance, or on an external HDD. In the following command, we create the folder in the home folder of git:

在Raspberry Pi上,使用git用户来创建它(是否通过SSH,但是您应该始终使用该用户来创建新的存储库)。 您可以在任意位置创建此文件夹:例如,在Raspberry Pi的SD卡上,或在外部HDD上 。 在以下命令中,我们在git的主文件夹中创建该文件夹:

mkdir /home/git/Hello-World.git

Notice the .git suffix in the folder’s name. This suffix is not mandatory, and is just a convention: on the server side, repositories have this suffix. It’s a convention you can find on GitHub, for instance.

请注意文件夹名称中的.git后缀。 此后缀不是强制性的,只是一个约定:在服务器端,存储库具有此后缀。 例如,您可以在GitHub上找到一个约定。

Now change your current directory to this new one:

现在将当前目录更改为这个新目录:

cd /home/git/Hello-World.git

Finally, we initialize the repository:

最后,我们初始化存储库:

git init --bare

The --bare option is here to indicate that we want to create a bare repository on the Raspberry Pi. Once again, it’s a convention: a bare repo doesn’t store the data the same way they are stored on a working repo. Bare repositories are not adapted to be working repositories, but they are perfect for a server. More information about bare repositories can be found in Git’s documentation.

--bare选项在这里表示我们要在Raspberry Pi上创建一个裸存储库。 再一次,这是一个约定:裸仓库不像存储在工作仓库中那样存储数据。 裸存储库不适用于工作存储库,但是它们非常适合服务器。 有关裸仓库的更多信息可以在Git的文档中找到。

在计算机上初始化仓库 (Initializing the repo on your computer)

On the computer you’ll use to work on your project, create a new folder (anywhere you want). In a terminal, go to this directory (with cd) and initialize the repo with the following command:

在用于处理项目的计算机上,创建一个新文件夹(任何位置)。 在终端中,转到此目录(使用cd ),并使用以下命令初始化存储库:

git init

Here we create a normal repository and not a bare one. That way, we’ll see files the way we expect to on our computer.

在这里,我们创建一个普通的存储库,而不是一个裸露的存储库。 这样,我们将以我们期望的方式在计算机上查看文件。

Our two empty repos need to communicate. To enable this communication, we’ll create a remote on the working repository. Still in the working repository’s folder on your computer (where you launched git init), type the following command:

我们两个空的存储库需要进行通信。 为了实现这种通信,我们将在工作库中创建一个远程服务器。 仍在计算机上工作存储库的文件夹中(启动git init ),键入以下命令:

git remote add pi git@XXX.XXX.XXX.XXX:/home/git/Hello-World.git

This command creates a new remote named pi (you can choose whatever name you want). The XXX.XXX.XXX.XXX must be replaced by the IP address of the Raspberry Pi (local or not). Finally, the /home/git/Hello-World.git part refers to the absolute path to the repository on the server, so think about adapting it!

此命令将创建一个新的名为pi遥控器(您可以选择所需的任何名称)。 XXX.XXX.XXX.XXX必须替换为Raspberry Pi的IP地址(本地或非本地)。 最后,/ /home/git/Hello-World.git部分引用服务器上存储库的绝对路径,因此请考虑对其进行修改!

使用存储库 (Working with the repositories)

Now our repo is ready to use. Create new files, edit them, remove some others, as always. When you’re ready to create your first commit, just do the usual:

现在我们的仓库可以使用了。 与往常一样,创建新文件,对其进行编辑,然后删除其他文件。 准备好创建第一次提交时,只需执行通常的操作:

git commit -m 'My first commit'

After a few commits, you’ll want to send your changes to your server. To do that, use the following command, which should sound familiar to you if you’ve already used something like GitHub:

几次提交后,您将想要将所做的更改发送到服务器。 为此,请使用以下命令,如果您已经使用过类似GitHub的命令,这听起来应该很熟悉:

git push pi master

In the above command, we send the master branch to the remote repository named pi. It’s the remote we created in the previous part, so think about changing its name if you chose another one!

在上面的命令中,我们将master分支发送到名为pi的远程存储库。 这是我们在上一部分中创建的遥控器,因此如果您选择其他名称,请考虑更改其名称!

Now, someone else with access to the server — or you on another computer — can download your changes and make commits. To download the latest changes and get an updated working repository on your computer, type the following command:

现在,有权访问服务器的其他人(或您在另一台计算机上)可以下载更改并进行提交。 要下载最新更改并在您的计算机上获取更新的工作存储库,请键入以下命令:

git pull pi master

克隆存储库 (Cloning a repository)

Bringing in a new user? Or adding yourself on another computer? The repository exists on the server, but it needs to be added to another computer. This is when it’s time to clone the repository.

引入新用户? 还是将自己添加到另一台计算机上? 该存储库位于服务器上,但是需要将其添加到另一台计算机上。 这是时候克隆存储库了。

Cloning a repo requires knowledge of where it lives — or, in other words, what its address is. As it’s your server, you should know it, and it’s the same as the one we used above:

克隆仓库需要知道它的住处,或者换句话说,它的地址是什么。 因为它是您的服务器,所以您应该知道它,并且它与我们上面使用的服务器相同:

git clone git@XXX.XXX.XXX.XXX:/home/git/Hello-World.git

By cloning a repository, a remote is automatically created, named origin, so you don’t have to create yours as we did above.

通过克隆存储库,将自动创建一个名为origin的远程服务器,因此您不必像上面那样创建您的远程服务器。

结束语 (Closing Words)

As said above, the server we just built is very simple, but it may well be enough for small or private projects. If you need more features, you should consider another tool, or something like a private GitHub repository.

如上所述,我们刚刚构建的服务器非常简单,但是对于小型或私人项目而言,这可能就足够了。 如果需要更多功能,则应考虑使用其他工具或类似私有GitHub存储库的工具。

Have you set up a Raspberry Pi as a Git server? Or done something else interesting with a Pi? Please tell us about it in the comments.

您是否已将Raspberry Pi设置为Git服务器? 还是用Pi做过其他有趣的事情? 请在评论中告诉我们。

翻译自: https://www.sitepoint.com/setting-up-your-raspberry-pi-as-a-git-server/

raspberry+git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值