2014-3-9 13:41:30 [显示全部楼层]
3678浏览
查看: 3678|回复: 0

利用树莓派搭建Git私有服务器

[复制链接]
本文由 极客范 - Boyd Wang 翻译自 MONKEY HACKS。欢迎加入极客翻译小组,同我们一道翻译与分享。转载请参见文章末尾处的要求。


最近我利用树莓派为我所有的Git仓库创建了一个Git私有服务器。我花费了一些时间来使每件事情正确工作,但现在它已经能跑起来了。

安装Git

首先你需要在树莓派上安装Git.

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1


[color=teal !important]sudo apt[color=rgb(0, 111, 224) !important]-[color=teal !important]get [color=teal !important]install [color=teal !important]wget git[color=rgb(0, 111, 224) !important]-core



这将会安装Git服务器和必要的客户端软件。

安装SSH

如果你还没有安装SSH,通过以下命令安装它:

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1


[color=teal !important]sudo apt[color=rgb(0, 111, 224) !important]-[color=teal !important]get [color=teal !important]install ssh



通过以下命令启动它:

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1


sudo[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]/etc[color=rgb(0, 111, 224) !important]/[color=rgb(0, 45, 122) !important]init[color=rgb(51, 51, 51) !important].d[color=rgb(0, 111, 224) !important]/[color=teal !important]ssh start



现在ssh已经运行起来了,但是一旦你重启树莓派,你还需要重新执行一遍上面命令。你可以通过执行一次下面的命令来解决这个问题:

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1


[color=teal !important]sudo update[color=rgb(0, 111, 224) !important]-[color=rgb(0, 45, 122) !important]rc[color=rgb(51, 51, 51) !important].d[color=rgb(0, 111, 224) !important] [color=teal !important]ssh defaults



当你重启树莓派,SSH应该能够自动启动了。你可以看看能不能通过SSH连接到树莓派,在你的Windows机器上使用Putty(Mac用户需要寻找替代的SSH客户端)。

在HostName文本框里输入树莓派的IP地址。通过以下命令找到你的树莓派IP:

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1


[color=rgb(0, 111, 224) !important]/sbin[color=rgb(0, 111, 224) !important]/ifconfig



找到inet addr:后面的就是IP。

改变主机名

这步不是必须的,但是我强烈推荐如果你正在,计划使用多台树莓派设备。

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1


[color=teal !important]sudo leafpad[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]/etc[color=rgb(0, 111, 224) !important]/hostname



输入你想要的主机名,然后保存文件。我的主机名是”gitpi”。
接着,输入下面的命令:

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1


[color=teal !important]sudo leafpad[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]/etc[color=rgb(0, 111, 224) !important]/hosts



替换所有”raspberrypi”为你上一步输入的新主机名。然后,重启你的树莓派

添加一个”Git”用户和组

接着我们来创建一个”Git”用户和用户组。注意/home/git是这个例子里我使用的文件夹。如果你想使用别的路径,替换下面命令里的”/home/git”。

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1


adduser[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]--system[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]--shell[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]/bin[color=rgb(0, 111, 224) !important]/bash[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]--gecos[color=rgb(0, 111, 224) !important] [color=rgb(221, 17, 68) !important]'git version control by pi'[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]--group[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]--home[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]/home[color=rgb(0, 111, 224) !important]/[color=teal !important]git git



接着是更改密码:

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1


[color=teal !important]passwd git



你的”git”用户现在有了一个新密码。现在尝试切换用户,你将会看到现在的终端提示用户名和主机为”git@gitpi”。你可以通过下面的命令切换用户:

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1


[color=teal !important]su git



增加一个空的Git仓库(Git Repository)

我们现在来增加一个空的Git仓库.
首先更改目录到你存储git的路径下。

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1


cd[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]/home[color=rgb(0, 111, 224) !important]/git



为你的仓库创建一个文件夹,并移动进去,然后初始化并清空仓库。
注意我现在使用的是”git”用户。这个用户具有/home/git目录的控制权。

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1

[color=rgb(170, 170, 170) !important]2

[color=rgb(170, 170, 170) !important]3


[color=teal !important]mkdir [color=rgb(0, 45, 122) !important]test[color=rgb(51, 51, 51) !important].[color=teal !important]git
[color=teal !important]cd [color=rgb(0, 45, 122) !important]test[color=rgb(51, 51, 51) !important].[color=teal !important]git
git[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]--[color=teal !important]bare init



Push你的代码到Pi上

最终,我们会把代码push到树莓派上。首先,更改路径到你之前初始化的git仓库(或初始化一个新的)。

加入一个新的远程主机(*你的IP地址没有中括号)

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1


[color=teal !important]git [color=teal !important]remote [color=teal !important]add [color=teal !important]pi git[color=rgb(51, 51, 51) !important]@[color=rgb(51, 51, 51) !important][[color=teal !important]your IP[color=rgb(0, 111, 224) !important]:[color=rgb(0, 111, 224) !important]/home[color=rgb(0, 111, 224) !important]/git[color=rgb(0, 111, 224) !important]/[color=rgb(0, 45, 122) !important]test[color=rgb(51, 51, 51) !important].git



现在你要做的就是add你的代码,commit然后push。

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1

[color=rgb(170, 170, 170) !important]2

[color=rgb(170, 170, 170) !important]3


[color=teal !important]git [color=rgb(0, 45, 122) !important]add[color=rgb(0, 111, 224) !important] [color=rgb(51, 51, 51) !important].
[color=teal !important]git commit[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]-am[color=rgb(0, 111, 224) !important] [color=rgb(221, 17, 68) !important]"Initial"
[color=teal !important]git [color=teal !important]push [color=teal !important]pi master



如果你得到了一个类似这样的消息”authenticity of host …”只需要输入”yes”然后继续就可以了。
理想情况下,如果一切正常,你的Git仓库已经搭建在你的树莓派上了。

如果你想要测试一下,试着clone你的仓库到你的Windows机器上。首先更改路径到你希望存储clone的地方(一个空文件夹),然后通过命令行(或git bash),运行:

[backcolor=rgb(248, 248, 255) !important]












[color=rgb(170, 170, 170) !important]1


[color=teal !important]git clone[color=rgb(0, 111, 224) !important] git[color=rgb(51, 51, 51) !important]@[color=rgb(51, 51, 51) !important][[color=teal !important]your IP[color=rgb(0, 111, 224) !important]:[color=rgb(0, 111, 224) !important]/home[color=rgb(0, 111, 224) !important]/git[color=rgb(0, 111, 224) !important]/[color=rgb(0, 45, 122) !important]test[color=rgb(51, 51, 51) !important].git



如果你有任何问题,请到原博客地址留言。祝你好运!



原文链接: MONKEY HACKS 翻译: 极客范- Boyd Wang

译文链接: http://www.geekfan.net/6228/
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail