Git入门操作

仅学习Git的一些入门操作比较容易,平时更多地使用GitHub,不过今天我想自个搭个服务练练手。当看完一些材料合作了一些验证之后,才发现其实所谓的服务和之前的svn完全不一样了。过程记录如下:

LinuxServer端安装git

我把服务打在Linux机器上,这台破机器没有apt-get,没有yum,只好自己下载源码,自己编译 

$ wget http://distfiles.macports.org/git/git-2.5.3.tar.gz
$ tar xzvf git-latest.tar.gz 
$ cd git-{date} 
$ autoconf 
$ ./configure --with-curl=/usr/local 
$ make 
$ make install

还好一路顺利:

$ git --version
git version 2.5.3

 

Server端ssh配置 

1)如果client端没有创建ssh rsa公钥,则先创建:

$ cd ~

$ ssh-keygen -t rsa    # 默认存放路径在~/.ssh/id_rsa.pub

2)如果client端已创建,并存在~/.ssh/id_rsa.pub,则将其拷贝到server端:

$ ssh <username>@<server_ip>:.ssh/authorized_keys

id_rsa.pub                                    100%  416     0.4KB/s   00:00 

3)修改Server端的sshd_config:

$ ssh <username>@<server_ip>
$ cd /etc            # 有的机器在/etc/ssh下面
$ sudo chmod 666 sshd_config

$ vi sshd_config
#PermitRootLogin yes     改为 PermitRootLogin no

# 下面几项把前面的 #注释 去掉
#RSAAuthentication yes
#PubkeyAuthentication yes
#AuthorizedKeysFile     .ssh/authorized_keys
#PasswordAuthentication no
#PermitEmptyPasswords no

#UsePAM yes          改为 UsePAM no 

 

测试是否有效

1)在Server端创建空的repository

$ cd repo

$ git init -—bare    # -—bare flag 只是用来存储pushes,不会当做本地repository来使用的。

2)在Client端创建repository 并push

$ mkdir localrepo
$ cd localrepo
$ git init
$ touch README
$ git add .
$ git commit -m “add README”
$ git remote add origin <username>@<server_ip>:/repo
$ git push origin master

3)在Client端clone刚刚创建的repository

$ git clone <username>@<server_ip>:/repo localrepo2

发现刚刚提交的代码树被clone下来,说明搭建成功

 

由于git是分布式的,没有主从之分,所以我理解所谓搭建git服务,其实就是确保有权限访问“服务器”上的git文件。这与该文件是在远程还是本地是无关的。比如上面的例子中,如果我把服务器放在本地,一样是可以的。

验证服务器搭建在本地

1)在本地创建空的repository

$ mkdir ~/repo
$ cd ~/repo
$ git init —bare

2)在本地创建repository 并push

$ mkdir ~/localrepo
$ cd ~/localrepo
$ git init
$ touch README
$ git add .
$ git commit -m “add README”
$ git remote add origin ~/repo
$ git push origin master

3)在本地clone刚刚创建的repository

$ git clone ~/repo ~/localrepo2

查看~/localrepo2,果然README被克隆过来了。

 

参考资料:

http://www.cnblogs.com/eileenleung/p/3503337.html

 

 

 

 

posted @ 2015-09-29 00:45  palance  阅读(270)  评论(0编辑  收藏  举报