分布式版本控制系统——Git

分布式相比于集中式的最大区别在于开发者能够将代码提交到本地,每一个开发者经过克隆,在本地机器上拷贝一个完整的git仓库。git

下图是经典的git开发过程:
分布式版本控制系统——Gitgithub

git的功能特性以下:服务器

  • 从服务器上克隆完整的git仓库(包括代码和版本信息)到单机上;
  • 在本身的机器上根据不一样的开发目的,建立分支,修改代码;
  • 在单机上本身建立的分支上提交代码;
  • 在单机上合并分支;
  • 把服务器上最新版的代码fetch下来,而后跟本身的主分支合并;
  • 生成补丁,把补丁发送给主开发者;

git能够安装在Windows、mac、Linux等操做系统之上,这里将写下如何安装在Linux系统之上,及其基本操做。ssh

1、安装git

很是简单,就一条命令,以下:分布式

[root@git ~]# yum -y install git

2、git库的建立及介绍

[root@git /]# mkdir /git
[root@git /]# cd git/
[root@git git]# git init             # 初始化为git库
Initialized empty Git reposi tory in /git/.git/
[root@git git]# ls -a                   #初始化成功后,会生成一个.git的隐藏目录
.  ..  .git
#生成的隐藏目录是用来跟踪管理版本库的,不建议随便修改其目录中的文件,
#若是改乱了,就把git库给破坏了。

在git版本库中,有三个重要的概念:工做区、暂存区、版本库。ide

  • 工做区:就是你的系统中能够看到的目录;
  • 暂存区:通常存放在.git目录下的index文件中,因此也会将暂存区叫作索引;
  • 版本库:工做区中的有一个.git隐藏目录,这个不算工做区,而是git的版本库。

下面这个图展现了工做区、版本库中的暂存区和版本库之间的关系:测试

分布式版本控制系统——Git
上图中,左侧为工做区,右侧为版本库,在版本库中标记为“index”的区域就是暂存区,标记为“master”的是master分支表明的目录树。fetch

当对工做区修改(或新增)的文件执行 "git add" 命令时,暂存区的目录树被更新,同时工做区修改(或新增)的文件内容被写入到对象库中的一个新的对象中,而该对象的ID被记录在暂存区的文件索引中。操作系统

当执行提交操做(git commit)时,暂存区的目录树写到版本库(对象库)中,master 分支会作相应的更新。即 master 指向的目录树就是提交时暂存区的目录树。3d

当执行 "git reset HEAD" 命令时,暂存区的目录树会被重写,被 master 分支指向的目录树所替换,可是工做区不受影响。

当执行 "git rm --cached <file>" 命令时,会直接从暂存区删除文件,工做区则不作出改变。

当执行 "git checkout ." 或者 "git checkout -- <file>" 命令时,会用暂存区所有或指定的文件替换工做区的文件。这个操做很危险,会清除工做区中未添加到暂存区的改动。

当执行 "git checkout HEAD ." 或者 "git checkout HEAD <file>" 命令时,会用 HEAD 指向的 master 分支中的所有或者部分文件替换暂存区和以及工做区中的文件。这个命令也是极具危险性的,由于不但会清除工做区中未提交的改动,也会清除暂存区中未提交的改动。

3、git库的基本操做

[root@git git]# git config --global user.name "zyz"
[root@git git]# git config --global user.email "zyz@zyz.com"
[root@git git]# echo "aaaa" > test.txt
[root@git git]# git add test.txt
[root@git git]# git commit -m "第一次提交"
#将暂存区的文件提交到版本库,而且必定要使用“-m”选项注明提交说明
[master (root-commit) 9ec7631] 第一次提交
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
[root@git git]# echo bbbb >> test.txt             # 修改测试文件
[root@git git]# git status            # 查看状态他会说咱们这个文件发生更改须要添加
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   test.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@git git]# git add test.txt                  # 添加到暂存区
[root@git git]# git status                 # 再次查看状态他会说须要再次提交这个文件
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   test.txt
#
[root@git git]# git commit -m "第二次提交"           # 提交完毕
[master 8c3c7db] 第二次提交
 1 file changed, 1 insertion(+)
[root@git git]# git status                   # 再次查看状态
# On branch master
nothing to commit, working directory clean
[root@git git]# echo cccc >> test.txt             # 修改第一个测试文件
[root@git git]# echo "第二个测试文件" > test2.txt         # 新建几个测试文件
[root@git git]# echo "第三个测试文件" > test3.txt
[root@git git]# git add test.txt test2.txt test3.txt            # 添加到暂存区
[root@git git]# git status            # 查看状态
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   test.txt                 # 修改
#   new file:   test2.txt                 # 新文件
#   new file:   test3.txt                 # 新文件
#
#上面是提示git被修改,而且添加了两个新的文件
[root@git git]# git commit -m "提交多个版本"                # 将暂存区的多个版本进行提交
[master 92e5155] 提交多个版本
 3 files changed, 3 insertions(+)
 create mode 100644 test2.txt
 create mode 100644 test3.txt
[root@git git]# git log --pretty=oneline          # #查看提交记录,一行对应一次提交记录
92e5155ccbb59913cd717a539a11529b786a564b 提交多个版本
8c3c7dbc6eb90d64899df1d23c0546fe1ffbe064 第二次提交
9ec7631c3c93990f71db83ae04e8b387aa1213d6 第一次提交
#至此,git库下的全部文件都被提交了,那么,我如今将本地的全部文件都删除,查看下git的状态是什么
[root@git git]# rm test*              #删除当前目录下全部的测试文件
[root@git git]# git status
# On branch master
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   deleted:    test.txt                  # 删除
#   deleted:    test2.txt
#   deleted:    test3.txt
#上述提示了删除了三个文件,下面说的是修改了可是还没有提交
no changes added to commit (use "git add" and/or "git commit -a")
#那么,我如今若想恢复删除的文件呢?只需进行如下操做:
[root@git git]# git reflog --pretty=oneline               # 查看 提交记录
92e5155 HEAD@{0}: commit: 提交多个版本
8c3c7db HEAD@{1}: commit: 第二次提交
9ec7631 HEAD@{2}: commit (initial): 第一次提交
[root@git git]# ls                    # 肯定没有任何测试文件
[root@git git]# git reset --hard 92e5155              # #版本回滚到指定提交的位置
HEAD is now at 92e5155 提交多个版本 
[root@git git]# ls                      #再次查看,被删除的文件又回来了
test2.txt  test3.txt  test.txt
#那么,如今我想要恢复到第一次提交的时候呢?
[root@git git]# git reflog --pretty=oneline             # 一样须要查看提交日志
92e5155 HEAD@{0}: commit: 提交多个版本
8c3c7db HEAD@{1}: commit: 第二次提交
9ec7631 HEAD@{2}: commit (initial): 第一次提交
[root@git git]# git reset --hard HEAD@{2}         #对,在版本回滚时,不但能够指定第一列的ID号,也能够指定其HEAD字段
HEAD is now at 9ec7631 第一次提交
[root@git git]# ls        #再次查看当前工做目录下,恢复到了最初只有一个测试文件的状态
test.txt 
[root@git git]# git reset --hard 92e5155                  #再次恢复到测试文件最多的时候
HEAD is now at 92e5155 提交多个版本
[root@git git]# ls
test2.txt  test3.txt  test.txt

4、撤销修改的操做

关于撤销修改,其实在上面已经展现出来如何从版本库中撤销修改了,那么下面将介绍如何从暂存区、工做台进行撤销修改
1.从工做台撤销修改

[root@git git]# cat test.txt             #肯定当前文件内容
aaaa
bbbb
cccc
[root@git git]# echo dddd >> test.txt               #修改文件内容
[root@git git]# cat test.txt                    # #查看修改后的文件
aaaa
bbbb
cccc
dddd
[root@git git]# git checkout -- test.txt                #对工做台执行撤销操做
[root@git git]# cat test.txt                #确认新添加的内容被撤销
aaaa
bbbb
cccc

2.从暂存区撤销修改

[root@git git]# echo "123123" > aaa.txt                    # 建立一个新的测试文件
[root@git git]# git add aaa.txt              # 添加到暂存区
[root@git git]# git status 
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   new file:   aaa.txt                  # 新文件
# 
[root@git git]# git reset HEAD aaa.txt              # 撤销
[root@git git]# git status        #再次查看git的状态,提示提交为空,还提示使用git add创建提交
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   aaa.txt                 # 这里已经显示不是新文件
nothing added to commit but untracked files present (use "git add" to track)
#提交为空,可是存在还没有跟踪的文件(使用 "git add" 创建跟踪)

3.从版本库中删除指定版本

[root@git git]# rm test.txt             #删除本地文件
root@git git]# git rm test.txt                #使用git执行rm命令
rm 'test.txt'
[root@git git]# git commit -m "删除文件"               #提交到版本库
[master f9bccab] 删除文件
 1 file changed, 3 deletions(-)
 delete mode 100644 test.txt

至此,都只是git版本库的基本操做,那么?咱们如何将咱们的git
库关联到github上呢?下面是两种状况下的关联方法。

5、将本地git库关联到github

状况一:本地有git库,github库是空的:
1.首先须要先建立一个空的github库。

自行注册github帐号并登录,比较简单,这里就不写了。

分布式版本控制系统——Git
分布式版本控制系统——Git
在主机上生成秘钥对,并上传至github上:

[root@git git]# ssh-keygen -t rsa -C "848369866@qq.com"            #执行此命令后,一路按回车便可,“-C”后面是本身的邮箱地址
[root@git git]# cat ~/.ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDEKmiwENbVxLSn2tRDUAFVh3muJFXJYqjuOPU+RfCIb42Bv2w52zzasY4rEPgPc865b+NTLk4Xkw4SEEl7qzObf/kDyqtM0d7PjCR+E4/u9MKHo+evgtNJwrNXeoycKc1EHKYWRqAbJ79QulwqDPU7Z2DC9I0ML1Kf6ipRe4biN58imyt/YLxVj9RnmWS6r+umkSM4ukn4DKHGfI1DJxIWEk2jwgQKBmdBB06YZvHBn6VOKaIlCSSHCAWZ51D6EDT8LdZ4jwZmbqN0ypBFG7E6KM5oYPE1hIRnbcy+HB7jYCptNmZhv2eamHH+yL+oMe2Wn/wxwc1gTRfl9Epz1LD7 848369866@qq.com

在github上操做以下,以便添加公钥:
分布式版本控制系统——Git
分布式版本控制系统——Git
分布式版本控制系统——Git
分布式版本控制系统——Git
输入github帐号的密码进行验证:
分布式版本控制系统——Git
肯定添加成功:
分布式版本控制系统——Git
2.回到新建立的库:
分布式版本控制系统——Git
分布式版本控制系统——Git

[root@git git]# ls -a
.  ..  aaa.txt  .git  test2.txt  test3.txt
[root@git git]# git remote add origin git@github.com:zhangyz-nb/test01.git          #执行提示的第一条命令
[root@git git]# git push -u origin master              #进行提交,因为是第一次上传,因此须要使用“-u”选项
#若是执行上述命令后,提示须要输入“yes”,输入便可
Counting objects: 13, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (13/13), 1021 bytes | 0 bytes/s, done.
Total 13 (delta 0), reused 0 (delta 0)
To git@github.com:zhangyz-nb/test01.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

#提示已经提交成功了。

至此,F5刷新咱们github上刚刚建立的库的页面,就能够看到咱们工做台目录下的那些文件了,以下:
分布式版本控制系统——Git

6、从github下载到本地git版本库

上述已经演示了如何将本地的git版本库关联到远端的github的空库。

那么这里将展现如何将github已存在的库(库中有内容)下载到本地。

因为在第五步操做时,已经设置好了邮箱及ssh秘钥等操做,因此这里就能够省略这两部操做了,若是没有配置邮箱及ssh秘钥,可参考第五个段落进行配置。

这里就将第五步建立的github库下载到本地。

找到github建立的库,以下:
分布式版本控制系统——Git
分布式版本控制系统——Git

[root@git git]# git clone git@github.com:zhangyz-nb/test01.git
Cloning into 'test01'...
#执行命令“git clone”,后面的路径就是咱们复制的github上的ssh路径
Warning: Permanently added the RSA host key for IP address '52.74.223.119' to the list of known hosts.
remote: Enumerating objects: 13, done.
remote: Counting objects: 100% (13/13), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 13 (delta 0), reused 13 (delta 0), pack-reused 0
Receiving objects: 100% (13/13), done.
[root@git git]# cd test01/
[root@git test01]# ls
test2.txt  test3.txt
[root@git test01]# echo "clone success..." > succed          # 建立一个新的测试文件
[root@git test01]# ls 
succed  test2.txt  test3.txt
[root@git test01]# git add succed        # 添加到暂存区
[root@git test01]# git commit -m "clone succes"         # 提交
[master 4b662bd] clone succes
 1 file changed, 1 insertion(+)
 create mode 100644 succed
[root@git test01]# git push origin master              # 将本地文件推送到github
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 306 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:zhangyz-nb/test01.git
   f9bccab..4b662bd  master -> master
[root@git test01]# git remote              # 查看远端版本库信息
origin

回到github上,刷新库的页面,便可看到新提交的文件,以下:
分布式版本控制系统——Git

相关文章
相关标签/搜索