-
GIT(分布式版本控制系统)
使用Git服务程序
在正式使用前,我们还需要弄清楚Git的三种重要模式,分别是已提交、已修改和已暂存:
已提交(committed):表示数据文件已经顺利提交到Git数据库中。
已修改(modified):表示数据文件已经被修改,但未被保存到Git数据库中。
已暂存(staged):表示数据文件已经被修改,并会在下次提交时提交到Git数据库中。
提交前的数据文件可能会被随意修改或丢失,但只要把文件快照顺利提交到Git数据库中,那就可以完全放心了,流程为:
1.在工作目录中修改数据文件。
2.将文件的快照放入暂存区域。
3.将暂存区域的文件快照提交到Git仓库中。
主机名称 操作系统 IP地址
GIT服务器 Centos 7 192.168.126.137
GIT客户端 Centos 7 192.168.126.138
[[email protected] ~]# yum install -y git Loaded plugins: langpacks, product-id, subscription-manager ………………省略部分安装过程……………… Installing: git x86_64 1.8.3.1-4.el7 rhel7 4.3 M Installing for dependencies: perl-Error noarch 1:0.17020-2.el7 rhel7 32 k perl-Git noarch 1.8.3.1-4.el7 rhel7 52 k perl-TermReadKey x86_64 2.30-20.el7 rhel7 31 k ………………省略部分安装过程……………… Complete!
然后创建Git版本仓库,一般规范的方式要以.git为后缀:
[[email protected] ~]# mkdir accommate.git
修改Git版本仓库的所有者与所有组:
[[email protected] ~]# chown -Rf git:git accommate.git/
初始化Git版本仓库:
[[email protected] ~]# cd accommate.git/
[[email protected] accommate.git]# git --bare init
Initialized empty Git repository in /root/accommate.git/
其实此时你的Git服务器就已经部署好了,但用户还不能向你推送数据,也不能克隆你的Git版本仓库,因为我们要在服务器上开放至少一种支持Git的协议,比如HTTP/HTTPS/SSH等,现在用的最多的就是HTTPS和SSH,我们切换至Git客户机来生成SSH密钥:
[[email protected] /]# 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: b0:74:b6:97:98:ad:a3:f0:24:a8:b6:f4:e5:d8:31:73 [email protected] The key‘s randomart image is: +--[ RSA 2048]----+ | | | | | o o | | . = = . | | . S + | | . o | | .. o=.Eo | |.o. ===. . | |o..o oo | +-----------------+
将客户机的公钥传递给Git服务器:
[[email protected] /]# ssh-copy-id 192.168.126.137 The authenticity of host ‘192.168.126.137 (192.168.126.137)‘ can‘t be established. ECDSA key fingerprint is 0a:89:0f:71:fb:0e:75:f7:37:9b:50:37:56:b7:95:25. Are you sure you want to continue connecting (yes/no)? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys [email protected]192.168.126.137‘s password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh ‘192.168.126.137‘" and check to make sure that only the key(s) you wanted were added.
此时就已经可以从Git服务器中克隆版本仓库了(此时目录内没有文件是正常的):
[[email protected] /]# git clone [email protected]192.168.126.137:/accommate.git Cloning into ‘accommate‘... warning: You appear to have cloned an empty repository.
向Git版本仓库提交一个新文件:
[[email protected] accommate]# echo "I successfully cloned the Git repository" > readme.txt [[email protected] accommate]# git add readme.txt [[email protected] accommate]# git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: readme.txt # [[email protected] accommate]# git commit -m "Clone the Git repository" [master (root-commit) 1709f48] Clone the Git repository 1 file changed, 1 insertion(+) create mode 100644 readme.txt [[email protected] accommate]# git status # On branch master nothing to commit, working directory clean
但是这次的操作还是只将文件提交到了本地的Git版本仓库,并没有推送到远程Git服务器,所以我们来定义下远程的Git服务器吧:
[[email protected] accommate]# git remote add server [email protected]192.168.126.137:/accommate.git
将文件提交到远程Git服务器吧:
[[email protected] accommate]# git push -u server master Counting objects: 3, done. Writing objects: 100% (3/3), 272 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To [email protected]192.168.126.137:/accommate.git * [new branch] master -> master Branch master set up to track remote branch master from server.
为了验证真的是推送到了远程的Git服务,你可以换个目录再克隆一份版本仓库(虽然在工作中毫无意义):
[[email protected] accommate]# cd ../Desktop [[email protected] accommate Desktop]# git clone [email protected]192.168.10.10:/root/accommate.git Cloning into ‘accommate‘... remote: Counting objects: 3, done. remote: Total 3 (delta 0), reused 0 (delta 0) Receiving objects: 100% (3/3), done. [[email protected] Desktop]# cd accommate/ [[email protected] linuxprobe]# cat readme.txt I successfully cloned the Git repository