zoukankan      html  css  js  c++  java
  • 使用Git分布式版本控制系统

     

    GIT(分布式版本控制系统)

      Git是一款免费、开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。
     
      Git是一个开源的分布式版本控制系统,可以有效、高速的处理从很小到非常大的项目版本管理。  Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。
     
      Torvalds 开始着手开发 Git 是为了作为一种过渡方案来替代 BitKeeper,后者之前一直是 Linux 内核开发人员在全球使用的主要源代码工具。开放源码社区中的有些人觉得BitKeeper 的许可证并不适合开放源码社区的工作,因此 Torvalds 决定着手研究许可证更为灵活的版本控制系统。尽管最初 Git 的开发是为了辅助 Linux 内核开发的过程,但是我们已经发现在很多其他自由软件项目中也使用了 Git。例如 很多 Freedesktop 的项目迁移到了 Git 上。
     
      
      分布式版本控制系统最大的特点是不需要每次提交都把文件推送到版本控制服务器,而是采用分布式版本库的机制,使得每个开发人员都够从服务器中克隆一份完整的版本库到自己计算机本地,不必再完全依赖于版本控制服务器,使得源代码的发布和合并更加方便,并且因为数据都在自己本地,不仅效率提高了,而且即便我们离开了网络依然可以执行提交文件、查看历史版本记录、创建分支等等操作,真的是开发者的福音啊。
     
      Git不仅是一款开源的分布式版本控制系统,而且有其独特的功能特性,例如大多数的分布式版本控制系统只会记录每次文件的变化,说白了就是只会关心文件的内容变化差异,而Git则是关注于文件数据整体的变化,直接会将文件提交时的数据保存成快照,而非仅记录差异内容,并且使用SHA-1加密算法保证数据的完整性。
     
      
    使用Git服务程序

    在正式使用前,我们还需要弄清楚Git的三种重要模式,分别是已提交、已修改和已暂存:

    已提交(committed):表示数据文件已经顺利提交到Git数据库中。

    已修改(modified):表示数据文件已经被修改,但未被保存到Git数据库中。

    已暂存(staged):表示数据文件已经被修改,并会在下次提交时提交到Git数据库中。

    提交前的数据文件可能会被随意修改或丢失,但只要把文件快照顺利提交到Git数据库中,那就可以完全放心了,流程为:

    1.在工作目录中修改数据文件。

    2.将文件的快照放入暂存区域。

    3.将暂存区域的文件快照提交到Git仓库中。

    部署Git服务器
     
      主机名称    操作系统         IP地址
      GIT服务器   Centos 7    192.168.126.137
      GIT客户端   Centos 7    192.168.126.138
     
     
      首先我们在Git服务器和客户端上安装Git服务程序
     
    [root@accommate ~]# 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为后缀:
    [root@accommate ~]# mkdir accommate.git

     修改Git版本仓库的所有者与所有组:

    [root@accommate ~]# chown -Rf git:git accommate.git/

     初始化Git版本仓库:

    [root@accommate ~]# cd accommate.git/
    [root@accommate accommate.git]# git --bare init
    Initialized empty Git repository in /root/accommate.git/

      其实此时你的Git服务器就已经部署好了,但用户还不能向你推送数据,也不能克隆你的Git版本仓库,因为我们要在服务器上开放至少一种支持Git的协议,比如HTTP/HTTPS/SSH等,现在用的最多的就是HTTPS和SSH,我们切换至Git客户机来生成SSH密钥:

    [root@localhost /]# 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 root@localhost.localdomain
    The key's randomart image is:
    +--[ RSA 2048]----+
    |                 |
    |                 |
    |      o o        |
    |     . = = .     |
    |      . S +      |
    |   .     o       |
    | .. o=.Eo        |
    |.o. ===. .       |
    |o..o oo          |
    +-----------------+

     将客户机的公钥传递给Git服务器:

    [root@localhost /]# 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
    root@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服务器中克隆版本仓库了(此时目录内没有文件是正常的):

    [root@localhost /]# git clone root@192.168.126.137:/accommate.git
    Cloning into 'accommate'...
    warning: You appear to have cloned an empty repository.

     向Git版本仓库提交一个新文件:

    [root@localhost accommate]# echo "I successfully cloned the Git repository" > readme.txt
    [root@localhost accommate]# git add readme.txt 
    [root@localhost accommate]# git status
    # On branch master
    #
    # Initial commit
    #
    # Changes to be committed:
    #   (use "git rm --cached <file>..." to unstage)
    #
    #    new file:   readme.txt
    #
    [root@localhost 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
    [root@localhost accommate]# git status
    # On branch master
    nothing to commit, working directory clean

     但是这次的操作还是只将文件提交到了本地的Git版本仓库,并没有推送到远程Git服务器,所以我们来定义下远程的Git服务器吧:

    [root@localhost accommate]# git remote add server root@192.168.126.137:/accommate.git

     将文件提交到远程Git服务器吧:

    [root@localhost 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 root@192.168.126.137:/accommate.git
     * [new branch]      master -> master
    Branch master set up to track remote branch master from server.

     为了验证真的是推送到了远程的Git服务,你可以换个目录再克隆一份版本仓库(虽然在工作中毫无意义):

    [root@localhost accommate]# cd ../Desktop
    [root@localhost accommate Desktop]# git clone root@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.
    [root@linuxprobe Desktop]# cd accommate/
    [root@linuxprobe linuxprobe]# cat readme.txt 
    I successfully cloned the Git repository


  • 相关阅读:
    [转]Apache Doris资料汇总
    [算法]最小差值
    如何在Mac上配置iTerm2以及给ITerm2配置lrzsz
    [算法]关于位运算
    [算法]LeetCode 152:乘积最大子序列
    [算法]LeetCode 120:三角形最小路径和
    奇淫巧技之程序启动后在进程列表中隐藏密码等关键信息
    真机调试时遇到“Could not launch *** process launch failed: Security”的解决办法
    Objective-C的 KVC和KVO
    开始python学习了
  • 原文地址:https://www.cnblogs.com/willamwang/p/8085194.html
Copyright © 2011-2022 走看看