分佈式版本控制系統——Git

分佈式相比於集中式的最大區別在於開發者可以將代碼提交到本地,每個開發者通過克隆,在本地機器上拷貝一個完整的git倉庫。

下圖是經典的git開發過程:
分佈式版本控制系統——Git

git的功能特性如下:

  • 從服務器上克隆完整的git倉庫(包括代碼和版本信息)到單機上;
  • 在自己的機器上根據不同的開發目的,創建分支,修改代碼;
  • 在單機上自己創建的分支上提交代碼;
  • 在單機上合併分支;
  • 把服務器上最新版的代碼fetch下來,然後跟自己的主分支合併;
  • 生成補丁,把補丁發送給主開發者;

git可以安裝在Windows、mac、Linux等操作系統之上,這裏將寫下如何安裝在Linux系統之上,及其基本操作。
一、安裝git
非常簡單,就一條命令,如下:

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

二、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版本庫中,有三個重要的概念:工作區、暫存區、版本庫。

  • 工作區:就是你的系統中可以看到的目錄;
  • 暫存區:一般存放在.git目錄下的index文件中,所以也會將暫存區叫做索引;
  • 版本庫:工作區中的有一個.git隱藏目錄,這個不算工作區,而是git的版本庫。

下面這個圖展示了工作區、版本庫中的暫存區和版本庫之間的關係:

分佈式版本控制系統——Git
上圖中,左側爲工作區,右側爲版本庫,在版本庫中標記爲“index”的區域就是暫存區,標記爲“master”的是master分支代表的目錄樹。

當對工作區修改(或新增)的文件執行 "git add" 命令時,暫存區的目錄樹被更新,同時工作區修改(或新增)的文件內容被寫入到對象庫中的一個新的對象中,而該對象的ID被記錄在暫存區的文件索引中。

當執行提交操作(git commit)時,暫存區的目錄樹寫到版本庫(對象庫)中,master 分支會做相應的更新。即 master 指向的目錄樹就是提交時暫存區的目錄樹。

當執行 "git reset HEAD" 命令時,暫存區的目錄樹會被重寫,被 master 分支指向的目錄樹所替換,但是工作區不受影響。

當執行 "git rm --cached <file>" 命令時,會直接從暫存區刪除文件,工作區則不做出改變。

當執行 "git checkout ." 或者 "git checkout -- <file>" 命令時,會用暫存區全部或指定的文件替換工作區的文件。這個操作很危險,會清除工作區中未添加到暫存區的改動。

當執行 "git checkout HEAD ." 或者 "git checkout HEAD <file>" 命令時,會用 HEAD 指向的 master 分支中的全部或者部分文件替換暫存區和以及工作區中的文件。這個命令也是極具危險性的,因爲不但會清除工作區中未提交的改動,也會清除暫存區中未提交的改動。
三、git庫的基本操作

[root@git git]# git config --global user.name "zyz"
[root@git git]# git config --global user.email "[email protected]"
[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

四、撤銷修改的操作
關於撤銷修改,其實在上面已經展示出來如何從版本庫中撤銷修改了,那麼下面將介紹如何從暫存區、工作臺進行撤銷修改
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上呢?下面是兩種情況下的關聯方法。
五、將本地git庫關聯到github
情況一:本地有git庫,github庫是空的:
1、首先需要先創建一個空的github庫。

自行註冊github賬號並登陸,比較簡單,這裏就不寫了。

分佈式版本控制系統——Git
分佈式版本控制系統——Git
在主機上生成祕鑰對,並上傳至github上:

[root@git git]# ssh-keygen -t rsa -C "[email protected]"            #執行此命令後,一路按回車即可,“-C”後面是自己的郵箱地址
[root@git git]# cat ~/.ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDEKmiwENbVxLSn2tRDUAFVh3muJFXJYqjuOPU+RfCIb42Bv2w52zzasY4rEPgPc865b+NTLk4Xkw4SEEl7qzObf/kDyqtM0d7PjCR+E4/u9MKHo+evgtNJwrNXeoycKc1EHKYWRqAbJ79QulwqDPU7Z2DC9I0ML1Kf6ipRe4biN58imyt/YLxVj9RnmWS6r+umkSM4ukn4DKHGfI1DJxIWEk2jwgQKBmdBB06YZvHBn6VOKaIlCSSHCAWZ51D6EDT8LdZ4jwZmbqN0ypBFG7E6KM5oYPE1hIRnbcy+HB7jYCptNmZhv2eamHH+yL+oMe2Wn/wxwc1gTRfl9Epz1LD7 [email protected]

在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 [email protected]: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 [email protected]:zhangyz-nb/test01.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

#提示已經提交成功了。

至此,F5刷新我們github上剛剛創建的庫的頁面,就可以看到我們工作臺目錄下的那些文件了,如下:
分佈式版本控制系統——Git
六、從github下載到本地git版本庫
上述已經演示瞭如何將本地的git版本庫關聯到遠端的github的空庫。

那麼這裏將展示如何將github已存在的庫(庫中有內容)下載到本地。

由於在第五步操作時,已經設置好了郵箱及ssh祕鑰等操作,所以這裏就可以省略這兩部操作了,如果沒有配置郵箱及ssh祕鑰,可參考第五個段落進行配置。

這裏就將第五步創建的github庫下載到本地。

找到github創建的庫,如下:
分佈式版本控制系統——Git
分佈式版本控制系統——Git

[root@git git]# git clone [email protected]: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 [email protected]:zhangyz-nb/test01.git
   f9bccab..4b662bd  master -> master
[root@git test01]# git remote              # 查看遠端版本庫信息
origin

回到github上,刷新庫的頁面,即可看到新提交的文件,如下:
分佈式版本控制系統——Git

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