Git和GitHub实践

软件开发离不开版本控制,开发N年,使用过的版本控制工具有很多,从VSS(有人评价此工具反人类设计吐舌头),到CVS,SVN都使用过。但是这些工具都是中心化的,随着互联网和技术的发展,分布式的版本控制工具也越来越多和流行,其中最有名的就是Git。集中式的版本管理工具最大的弊端在与一旦崩溃全部的工程相关人员都会受到影响,而分布式版本管理每人机器上都是一个完整的版本。此外Git 在版本分支的管理上也被使用者称道。

GitHub是一个开放的基于Git的互联网版本库,下面描述一下Git和GitHub实践

一、本次实践的目标和内容:

1、搭建linux环境下的Git使用

2、Git基本命令

3、使用Git和GitHub

4、搭建私有Git库

二、Git使用和命令介绍

1、在linux 下,确认Git是否有安装,输入  git 指令,如果出现如下帮助界面,说明git已经安装。 如果没有安装通过yum方式进行安装:  yum  install  git

2、 创建git库用户,在root账号下,使用如下 命令  

创建用户:  adduser  git

修改用户密码:    passwd  git

3、为git工具创建用户

  git config --global user.email "输入注册邮箱"
git config --global user.name " 输入注册用户 "

4、Git初始化

创建Git的存放目录,选择库存放的目录: /opt/cwqsologit

在这个目录下执行指令: git  init

本地库使用 git init ,如果是创建远程库,使用 git  init --bare


5、Git的上传文件和提交

在此目录下建立一个readme 文件,将此文件上传到 Git中

[root@cwqsolo cwqsologit]# git  add  readme

[root@cwqsolo cwqsologit]# git  commit  -m   readme

[root@cwqsolo cwqsologit]# git add readme
[root@cwqsolo cwqsologit]# git commit -m  "append 备注"
[master 658169e] append备注
 1 files changed, 3 insertions(+), 0 deletions(-)

通过git log 命令可以看到一串字符,如:c05f4b5bc3776aa50d879c03e8568b0b721715df  是通过SHA1计算出来,在分布式版本管理中,这个表示提交的ID不能像SVN那样简单。现在编辑一下readme,增加一下新版本

说明:
  此Git库创建于2018年01月

维护人员:
  cwqsolo

备注:
  只是测试

end:

再次add 并commit,通过git log 我们可以看到新增了一段记录,里面有一段新的字符串

6、Git的版本恢复

 
 
[root@cwqsolo cwqsologit]# git  reflog
38f7635 HEAD@{0}: commit: append end
658169e HEAD@{1}: commit: append备注
c05f4b5 HEAD@{2}: commit (initial): readme

我们现在先回退到第一个版本,然后再“吃后悔药”回到最后一个版本
[root@cwqsolo cwqsologit]# 
[root@cwqsolo cwqsologit]# git  reset  --hard  c05f4b5
HEAD is now at c05f4b5 readme
[root@cwqsolo cwqsologit]# ls
readme
[root@cwqsolo cwqsologit]# cat  readme
说明:
  此Git库创建于2018年01月

维护人员:
  cwqsolo
[root@cwqsolo cwqsologit]# git  reset  --hard  38f7635
HEAD is now at 38f7635 append  end
[root@cwqsolo cwqsologit]# 
[root@cwqsolo cwqsologit]# 
[root@cwqsolo cwqsologit]# cat  readme
说明:
  此Git库创建于2018年01月

维护人员:
  cwqsolo

备注:
  只是测试

end:

上述操作了说明了git,进行版本的各种回退和恢复是非常方便和容易的

7、工作区和暂存区

下面通过一些操作来理解一下Git的工作区和暂存区的概念。新建一个文件,

[root@cwqsolo cwqsologit]# vi  my.conf

logpath=/opt/dev
filename=bi.log
然后修改一下readme, 在end 后面加一个时间,然后通过git  status命令来看一下
[root@cwqsolo cwqsologit]# git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   readme
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       my.conf
no changes added to commit (use "git add" and/or "git commit -a")
使用git add 命令,将两个文件加入后
[root@cwqsolo cwqsologit]# git  status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   my.conf
#       modified:   readme
#
这个时候从工作区,存入到暂存区,通过commit指令可以将 暂存区提交到master
[root@cwqsolo cwqsologit]# git  commit -m  "two files"
[master 4327027] two files
 2 files changed, 3 insertions(+), 1 deletions(-)
 create mode 100644 my.conf
[root@cwqsolo cwqsologit]# 
[root@cwqsolo cwqsologit]# 
[root@cwqsolo cwqsologit]# git status
# On branch master
nothing to commit (working directory clean)
[root@cwqsolo cwqsologit]# 
commit后,工作区又被清理干净了。 工作区-》stage(暂存区)-》master

8、Git的文件删除

rm: remove regular file `my.conf'? y
[root@cwqsolo cwqsologit]# ls  -ltr
total 4
-rw-r--r-- 1 root root 110 Feb 28 22:02 readme
[root@cwqsolo cwqsologit]# 
[root@cwqsolo cwqsologit]# 
[root@cwqsolo cwqsologit]# 
[root@cwqsolo cwqsologit]# git  status
# On branch master
# Changed but not updated:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       deleted:    my.conf
#
no changes added to commit (use "git add" and/or "git commit -a")

1) 误删除,需要将my.conf 重新取回,可以执行下面命令 git  checkout --file my.conf
[root@cwqsolo cwqsologit]# git  checkout  --  my.conf
[root@cwqsolo cwqsologit]# 
[root@cwqsolo cwqsologit]# 
[root@cwqsolo cwqsologit]# ls
my.conf  readme

2)真实删除,我们要删除库里面的 my.conf
[root@cwqsolo cwqsologit]# git  rm  my.conf
rm 'my.conf'
[root@cwqsolo cwqsologit]# git  commit  -m "del  my.conf"
[master 245d7d4] del  my.conf
 1 files changed, 0 insertions(+), 2 deletions(-)
 delete mode 100644 my.conf
[root@cwqsolo cwqsologit]# 
[root@cwqsolo cwqsologit]# git  status
# On branch master
nothing to commit (working directory clean)

此外Git还有push,pull  ,remote 等指令,在后续实践中会使用

三、GitHub使用

前面都是在本地建立仓库和操作,但是说好的分布式呢?大家之间如何方便的协同呢?这就是GitHub,是一个互联网提供的Git仓库托管服务,只要注册一个GitHub账号,就可以获得Git远程仓库,好处:

1)对于随时随地开发的码农来说,互联网的仓库操作更方便(当然安全性问题)

2)不用考虑本地仓库占用硬盘

劣势: 互联网上的内容,公开,所以不能把安全要求高的放到GitHub上。所以GitHub对于开源软件来说简直就是天生的量身定制。GitHub的网址为  https://github.com/

在弹出的页面下,我们创建一个自己的库

点击创建后,生成自己的库,并且在这个页面上提供了库的地址。注意这是互联网库,提交的时候,不要把企业机密提交上去。

个页面同时提供了操作指南建议大家都看看

git init
git add README.md
git commit -m "first commit"
git remote add origin [email protected]:cwqsolo/study.git
git push -u origin master 


四、私有Git仓库实践

开发企业级应用,都会考虑代码安全问题,所以有必要在企业内部搭建GIt服务器,场景描述如下:

私有库环境:两台机器: 192.168.136.144  和 192.168.136.177, 都是centos操作系统,分别为6.5和7.0

建立私有库目标:模拟在144上建立git服务器仓库,然后在144和177建立git本地仓库,最后在144和177本地仓库中生成文件,并推送到服务器仓库

搭建私有git服务器的推荐步骤如下

1、相关服务144,177安装Git 

centos 安装可以通过yum方式:   yum  install  git。安装成功后执行git 指令,弹出下列界面说明安装成功。


2、创建用户和生产密码

在144和177上都创建git用户

adduser  git

passwd  git  根据界面提示输入2次密码 git1qaz

3、收集客户机的密钥,存放到git服务器,并且加入到git的key 文件中

1) 在客户机生成密钥  ,举例 

144主机上  ssh-keygen -t rsa -C "[email protected]"

177主机上  ssh-keygen -t rsa -C "[email protected]"

2) 将177生成的密码拷贝到144 git服务器上

3) 将密码文件追加到git服务器    /home/git/.ssh/authorized_keys 文件

下面这步在git服务器上,用git用户操作,cat id_dsa.pub >> ~/.ssh/authorized_keys

4、服务器仓库创建并初始化

在144上创建目录 /opt/cwqsolo.git   这个目录作为服务器仓库,进入这个目录,使用如下命令进行初始化

git  init  --bare  或者  git  init  --bare  /opt/cwqsolo.git

操作目录在/opt/cwqsolo.git下时,两条指令是一样的。

5、本地仓库创建并初始化

144上:创建目录 /opt/local144.git   ,进入这个目录,采用命令  git init 进行初始化(这里没有带 --bare参数)

177上:创建目录 /opt/local177.git   ,进入这个目录,采用命令  git init 进行初始化(这里没有带 --bare参数)

6、修改各个仓库的权限

在114上执行

chown -R  git:git  /opt/cwqsolo.git

chown -R  git:git  /opt/local144.git

在177上执行  

chown -R  git:git  /opt/local177.git

上述操作将这些目录的权限都赋给 git用户
7、在本地目录指定对应的服务器仓库

执行如下命令可以指定远程服务器仓库: git remote add origin [email protected]/opt/cwqsolo.git

8、 177上新增一个文件 my.conf  推入到仓库

git add  my.conf

git  commit -m "new file my.conf"

git  push  ssh://[email protected]/opt/cwqsolo.git  master

9、在144上新增一个文件并推入到仓库

touch  readme

git  add readme

git  commit  -m "add readme"

git  push  ssh://[email protected]/opt/cwqsolo.git  master

这个时候,因为177已经对库进行修改,git 会提示要先进行meger,才能提交。我们先要将库里的内容pull回本地库,然后重新push上去。pull会本地库命令如下: git pull  ssh://[email protected]/opt/cwqsolo.git  master

[git@cwqsolo local144.git]$ git pull  ssh://[email protected]/opt/cwqsolo.git  master
[email protected]'s password: 
From ssh://192.168.136.144/opt/cwqsolo
 * branch            master     -> FETCH_HEAD
Merge made by recursive.
 my.conf |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 my.conf
git push ssh://[email protected]/opt/cwqsolo.git  master
[git@cwqsolo local144.git]$ git push ssh://[email protected]/opt/cwqsolo.git  master
[email protected]'s password: 
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (5/5), 488 bytes, done.
Total 5 (delta 0), reused 0 (delta 0)
To ssh://[email protected]/opt/cwqsolo.git
   ebbe07e..382879a  master -> master
10、177上pull回144的修改
[git@dev-177 local177.git]$ git pull  ssh://[email protected]/opt/cwqsolo.git  master
[email protected]'s password: 
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 5 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (5/5), done.
来自 ssh://192.168.136.144/opt/cwqsolo
 * branch            master     -> FETCH_HEAD
更新 ebbe07e..382879a
Fast-forward
 readme | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 readme
[git@dev-177 local177.git]$ ls
my.conf  readme
[git@dev-177 local177.git]$ 
从ls 命令可以看到177本地库也从私有库上同步回更新的内容了。



猜你喜欢

转载自blog.csdn.net/csolo/article/details/79398669