使用Git分布式控制系统

问题1:为什么要有这个Git?
个人理解:开发者们通过邮件向Linus发送着自己编写的源代码文件,由Linus本人通过手工的方式将代码合并。能不能每个人本地都有一个服务端的源码数据库,这样不必再完全依赖于服务端的源码数据库,使得源代码的发布和合并更加方便。所以git诞生了!

实例:GitHub是通过Git进行版本控制的软件源代码托管服务,用户可以免费创建公开的代码仓库。(程序员都知道吧!)

问题2:什么是Git?
答:Git 是一个分布式版本控制软件,与CVS、Subversion一类的集中式版本控制工具不同,它采用了分布式版本库的作法,不需要服务器端软件,就可以运作版本控制,使得源代码的发布和交流极其方便!

一:Git服务程序运行思路
文字:
1.在工作目录中修改数据文件。
2.将文件的快照放入暂存区域。
3.将暂存区域的文件快照提交到Git仓库中。
图片:
在这里插入图片描述

二:Git服务基础命令:
个人的用户名称和电子邮件地址:
git config --global user.name “Name”
git config --global user.email “root@email”
提交数据:
git add readme.txt
将暂存区的文件提交到Git版本仓库:
git commit -m “解释说明”
查看下当前Git版本仓库的状态:
git status

三:部署Git服务器
Git是分布式的版本控制系统,我们只要有了一个原始的Git仓库,就可以让其他主机克隆我们的原始版本仓库,从而使得一个Git版本仓库可以被同时分布到不同的主机之上。
1.安装git服务
[root@caq ~]# yum install git
2.创建Git版本仓库,要以.git为后缀:
[root@caq ~]# mkdir caq.git
3.修改Git版本仓库的所有者与所有组
[root@caq ~]# chown -Rf git:git caq.git/
4.初始化Git版本仓库:
[root@caq ~]# cd caq.git/
[root@caq caq.git]# git --bare init Initialized empty Git repository in /root/caq.git/
5.服务器上的Git仓库此时已经部署好了,但是还不能分享给其他人。需要有个协议来限制。猜到了吧,是ssh协议!
在客户端生成ssh密钥
[root@caq ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory ‘/root/.ssh’.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
65:4a:53:0d:4f:ee:49:4f:94:24:82:16:7a:dd:1f:28 [email protected]
The key’s randomart image is:
±-[ RSA 2048]----+
| .o+oo.o. |
| .oo *.+. |
| …+ E * o |
| o = + = . |
| S o o |
| |
| |
| |
| |
±----------------+
将客户机的公钥传递给Git服务器:
[root@caq ~]# ssh-copy-id 192.168.10.10 [email protected]’s password: Number of key(s) added: 1 Now try logging into the machine, with: “ssh ‘192.168.10.10’” and check to make sure that only the key(s) you wanted were added.

6.现在客户端就可以克隆服务端的版本仓库了。
[root@caq ~]# git clone [email protected]:/root/caq.git Cloning into ‘caq’… warning: You appear to have cloned an empty repository.

7.初始化下Git工作环境:
[root@caq ~]# git config --global user.name “Liu Chuan” [root@caq ~]# git config --global user.email “[email protected]” [root@caq ~]# git config --global core.editor vim
8.向Git版本仓库中提交一个新文件:
[root@caq caq]# echo “I am fine” > readme.txt
[root@caq caq]# git add readme.txt
[root@caq caq]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use “git rm --cached …” to unstage)
#
# new file: readme.txt
#
[root@caq caq ]# git commit -m “Clone the Git repository”
[master (root-commit) c3961c9] Clone the Git repository
Committer: root
1 file changed, 1 insertion(+)
create mode 100644 readme.txt
[root@linuxprobe caq]# git status
# On branch master
nothing to commit, working directory clean

9.向服务器发送文件
[root@caq caq]# git remote add server [email protected]:/root/caq.git
[root@caq caq]# git push -u server master
Counting objects: 3, done.
Writing objects: 100% (3/3), 261 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:/root/caq.git
[new branch] master -> master
Branch master set up to track remote branch master from server.
10.查看远程服务器的仓库有无客户端发来的文件(我是又克隆了一份,当然也可以直接切换到服务器共享仓库查看)
[root@caq caq]# cd …/~
[root@caq ~]# git clone [email protected]:/root/caq.git
Cloning into ‘caq’…
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
[root@caq ~]# cd caq/
[root@caq caq]# cat readme.txt I am fine

git内容知识点很多,这篇要是看的人多的话我在出剩余的知识吧。偷个懒,嘻嘻嘻。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章