这里有新鲜出炉的精品教程, 程序狗速度看过来!
CentOS Linux 操作系统
CentOS(Community Enterprise Operating System, 中文意思是: 社区企业操作系统) 是 Linux 发行版之一, 它是来自于 Red Hat Enterprise Linux 依照开放源代码规定释出的源代码所编译而成
本篇文章主要介绍了在 CentOS 搭建 Git 服务器的详细步骤, 小编觉得挺不错的, 现在分享给大家, 也给大家做个参考一起跟随小编过来看看吧
前言
我们可以 GitHub 发布一些开源代码的公共仓库, 但对于私密仓库就需要收费了公司内部通常会搭建自己的 Git 服务器, 我也通过在自己的服务器上搭建练习一下
开始前先说一下服务器信息, 这里是阿里云的 CentOS 6.5 64 位操作系统
一 确认服务器是否安装 Git
- [root@iZ25r8k6ifuZ git]# rpm -qa git
- git-1.7.1-3.el6_4.1.x86_64
这里也已经安装过了, 如果没有安装可以用 yum install git 安装
二 创建 git 用户
这里你可以选择新建一个用户来测试, 也可以直接使用你的 root 进行以下操作笔者也是看着资料一步一步来的, 这里创建一个新用户 teslachen 进行操作
- [root@iZ25r8k6ifuZ ~]# useradd tesla
- [root@iZ25r8k6ifuZ ~]# passwd tesla
更改用户 tesla 的密码
新的 密码:
无效的密码: 它没有包含足够的不同字符
无效的密码: 过于简单
重新输入新的 密码:
passwd: 所有的身份验证令牌已经成功更新
注 1: 创建用户权限不够请加上 sudo;
注 2: 设置用户密码太过简单的话会有提示, 但依旧可以设置成功
三 生成 ssh 公钥
许多 Git 服务器都使用 SSH 公钥进行认证 为了向 Git 服务器提供 SSH 公钥, 如果某系统用户尚未拥有密钥, 必须事先为其生成一份
linux 可以在本机运行 ssh-keygen -t rsa 生成密钥, 把. pub 文件拷到服务器上
- [root@iZ25r8k6ifuZ ~]# su tesla
- [tesla@iZ25r8k6ifuZ root]$ cd ~
- [tesla@iZ25r8k6ifuZ ~]$ mkdir .ssh
- [tesla@iZ25r8k6ifuZ ~]$ ssh-keygen -t rsa
- Generating public/private rsa key pair.
- Enter file in which to save the key (/home/tesla/.ssh/id_rsa):
- Enter passphrase (empty for no passphrase):
- Enter same passphrase again:
- Your identification has been saved in /home/tesla/.ssh/id_rsa.
- Your public key has been saved in /home/tesla/.ssh/id_rsa.pub.
- The key fingerprint is:
- 13:bf:75:ba:67:7f:0e:a0:47:7a:fe:25:bc:81:85:c3 tesla@iZ25r8k6ifuZ
- The key's randomart image is:
- +--[ RSA 2048]----+
- | |
- | |
- | . |
- | o . . |
- | S . E o |
- | . O |
- | + = = .|
- | + .o.|
- | o+oo+|
- +-----------------+
- [tesla@iZ25r8k6ifuZ ~]$ cd .ssh/
- [tesla@iZ25r8k6ifuZ .ssh]$ cat id_rsa.pub>> ~/.ssh/authorized_keys
- exit
四 添加 tesla 到 sudoers 文件
tesla 用户现在对一些文件夹没有操作权限, 修改 / etc/sudoers 文件来改变他的权限最高管理员用户用下面命令打开
[root@iZ25r8k6ifuZ ~]# visudo
然后我们在 vim 中找到下面这行
root ALL=(ALL) ALL
按 i 键开始插入, 回车一下在下面一行加上
tesla ALL=(ALL) ALL
接着按下 esc 键, 输入 :wq , 回车保存退出
五 创建 Git 代码仓库
- [root@iZ25r8k6ifuZ ~]# mkdir /teslaRepo
- [root@iZ25r8k6ifuZ ~]# cd /teslaRepo/
- [root@iZ25r8k6ifuZ teslaRepo]# sudo mkdir teslaProject.git
- [root@iZ25r8k6ifuZ teslaRepo]# chown tesla:tesla /teslaRepo/
- [root@iZ25r8k6ifuZ teslaRepo]# chown -R tesla:git /teslaRepo/
- [root@iZ25r8k6ifuZ teslaRepo]# cd teslaProject.git/
- [root@iZ25r8k6ifuZ teslaProject.git]# sudo git --bare init
- Initialized empty Git repository in /teslaRepo/teslaProject.git/
这样一个叫 teslaProject 得 Git 仓库就创建好了
六 本地测试使用
你可以直接在服务器上进行本地测试, 也可以直接用你的电脑来测试下面我是使用自己的 MBP 来进行的测试
- localhost:~ okay$ cd Desktop/git/
- localhost:git okay$ mkdir teslaRepo
- localhost:git okay$ cd teslaRepo/
- localhost:teslaRepo okay$ git init
- Initialized empty Git repository in /Users/okay/Desktop/git/teslaRepo/.git/
- localhost:teslaRepo okay$ git remote add origin tesla@123.57.159.74:/teslaRepo/teslaProject.git
上面的命令在本地创建了一个文件夹并添加了服务器上的远程仓库
- localhost:teslaRepo okay$ touch a.txt
- localhost:teslaRepo okay$ git add a.txt
- localhost:teslaRepo okay$ git commit -m "init commit"
- [master (root-commit) d14cd3b] init commit
- 1 file changed, 0 insertions(+), 0 deletions(-)
- create mode 100644 a.txt
上面的命令在本地创建了一个 a.txt 并在本地提交了一次
- localhost:teslaRepo okay$ git push origin master
- tesla@123.57.159.74's password:
- Counting objects: 3, done.
- Writing objects: 100% (3/3), 202 bytes | 0 bytes/s, done.
- Total 3 (delta 0), reused 0 (delta 0)
- To tesla@123.57.159.74:/teslaRepo/teslaProject.git
- * [new branch] master -> master
上面的命令将本地代码 push 到远程服务器上去了, 下面我们在本地 clone 一次看下是否正确
七 本地 clone
- localhost:git okay$ mkdir ttt
- localhost:git okay$ cd ttt
- localhost:ttt okay$ git clone tesla@123.57.159.74:/teslaRepo/teslaProject.git
- Cloning into 'teslaProject'...
- tesla@123.57.159.74's password:
- remote: Counting objects: 3, done.
- remote: Total 3 (delta 0), reused 0 (delta 0)
- Receiving objects: 100% (3/3), done.
- Checking connectivity... done.
clone 完成, 让我们看一下文件夹目录
之前 push 到服务器上的 a.txt 文件已经被 clone 下来
------------ 分割线 -------------
1. 查看系统用户组
-d: 指定字段的分隔符, 默认的字段分隔符为 TAB;
-f: 显示指定字段的内容;
cut -d: -f1 /etc/group
2. 查看系统用户
cut -d: -f1 /etc/passwd
3. clone 仓库
git clone git@your_gitServer_ip:/home/gitrepo/sample.git
4. push 已有仓库
- // 以 master 分支示范
- git checkout master
- git remote rm origin
- git remote add origin git@your_gitServer_ip:/home/gitrepo/sample.git
- git push -u origin master
来源: http://www.phperz.com/article/18/0314/361451.html