Git和GitHub實踐__Git

來源:互聯網
上載者:User

軟體開發離不開版本控制,開發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  reflog38f7635 HEAD@{0}: commit: append end658169e HEAD@{1}: commit: append備忘c05f4b5 HEAD@{2}: commit (initial): readme

我們現在先回退到第一個版本,然後再“吃後悔藥”回到最後一個版本
[root@cwqsolo cwqsologit]# [root@cwqsolo cwqsologit]# git  reset  --hard  c05f4b5HEAD is now at c05f4b5 readme[root@cwqsolo cwqsologit]# lsreadme[root@cwqsolo cwqsologit]# cat  readme說明:  此Git庫建立於2018年01月維護人員:  cwqsolo[root@cwqsolo cwqsologit]# git  reset  --hard  38f7635HEAD 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.conflogpath=/opt/devfilename=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.confno 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 masternothing to commit (working directory clean)[root@cwqsolo cwqsologit]# 
commit後,工作區又被清理乾淨了。 工作區-》stage(暫存區)-》master

8、Git的檔案刪除

rm: remove regular file `my.conf'? y[root@cwqsolo cwqsologit]# ls  -ltrtotal 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]# lsmy.conf  readme

2)真實刪除,我們要刪除庫裡面的 my.conf
[root@cwqsolo cwqsologit]# git  rm  my.confrm '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 masternothing to commit (working directory clean)

此外Git還有push,pull  ,remote 等指令,在後續實踐中會使用

三、GitHub使用

前面都是在本地建立倉庫和操作,但是說好的分布式呢。大家之間如何方便的協同呢。這就是GitHub,是一個互連網提供的Git倉庫託管服務,只要註冊一個GitHub帳號,就可以獲得Git遠程倉庫,好處:

1)對於隨時隨地開發的碼農來說,互連網的倉庫操作更方便(當然安全性問題)

2)不用考慮本地倉庫佔用硬碟

劣勢: 互連網上的內容,公開,所以不能把安全要求高的放到GitHub上。所以GitHub對於開源軟體來說簡直就是天生的量身定製。GitHub的網址為  https://github.com/

在彈出的頁面下,我們建立一個自己的庫

點擊建立後,產生自己的庫,並且在這個頁面上提供了庫的地址。注意這是互連網庫,提交的時候,不要把企業機密提交上去。

這個頁面同時提供了操作指南建議大家都看看

git initgit add README.mdgit commit -m "first commit"git remote add origin git@github.com:cwqsolo/study.gitgit 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 "cwqsolo@163.com"

177主機上  ssh-keygen -t rsa -C "177user@qq.com"

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 git@192.168.136.144/opt/cwqsolo.git

8、 177上新增一個檔案 my.conf  推入到倉庫

git add  my.conf

git  commit -m "new file my.conf"

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.