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本地庫也從私有庫上同步回更新的內容了。



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