Git使用詳細教程(上篇)

Git是什麼?

Git是目前世界上最先進的分佈式版本控制系統。

工作原理 / 流程:

Workspace:工作區
Index / Stage:暫存區
Repository:倉庫區(或本地倉庫)
Remote:遠程倉庫

SVN與Git的最主要的區別?

SVN是集中式版本控制系統,版本庫是集中放在中央服務器的,而幹活的時候,用的都是自己的電腦,所以首先要從中央服務器哪裏得到最新的版本,然後幹活,幹完後,需要把自己做完的活推送到中央服務器。集中式版本控制系統是必須聯網才能工作,如果在局域網還可以,帶寬夠大,速度夠快,如果在互聯網下,如果網速慢的話,就納悶了。

Git是分佈式版本控制系統,那麼它就沒有中央服務器的,每個人的電腦就是一個完整的版本庫,這樣,工作的時候就不需要聯網了,因爲版本都是在自己的電腦上。既然每個人的電腦都有一個完整的版本庫,那多個人如何協作呢?比如說自己在電腦上改了文件A,其他人也在電腦上改了文件A,這時,你們兩之間只需把各自的修改推送給對方,就可以互相看到對方的修改了。

如何安裝Git

windows下安裝:需要從網上下載一個,然後進行默認安裝即可。安裝完成後,在開始菜單裏面找到 "Git --> Git Bash"
linux下安裝:直接使用yum源安裝就可以了。

如何操作?

創建版本庫

什麼是版本庫?版本庫又名倉庫,英文名repository,你可以簡單的理解一個目錄,這個目錄裏面的所有文件都可以被Git管理起來,每個文件的修改,刪除,Git都能跟蹤,以便任何時刻都可以追蹤歷史,或者在將來某個時刻還可以將文件”還原”。

所有創建一個版本庫是非常簡單,我在linux主機/home/目錄下,執行mkdir testgit創建一個testgit版本庫。

初始化版本庫

進入testgit目錄下執行git init。把這個目錄變成git可以管理的創庫,如下:

[root@node1 home]# cd testgit/
[root@node1 testgit]# pwd
/home/testgit
[root@node1 testgit]# git init
初始化空的 Git 版本庫於 /home/testgit/.git/
[root@node1 testgit]# ll -a
總用量 12
drwxr-xr-x   3 root root 4096 12月 27 14:46 .
drwxr-xr-x. 12 root root 4096 12月 27 14:45 ..
drwxr-xr-x   7 root root 4096 12月 27 14:46 .git

首先要明確下,所有的版本控制系統,只能跟蹤文本文件的改動,比如txt文件,網頁,所有程序的代碼等,Git也不列外,版本控制系統可以告訴你每次的改動,但是圖片,視頻這些二進制文件,雖能也能由版本控制系統管理,但沒法跟蹤文件的變化,只能把二進制文件每次改動串起來,也就是知道圖片從1kb變成2kb,但是到底改了啥,版本控制也不知道。

實例演示

下面通過一個簡單的demo演示,告訴大家一些git常用的命令。

我再版本庫testgit目錄下新建一個readme.txt 內容添加:this is test.

第一步:使用命令 git add readme.txt添加到暫存區裏面去。如下:

[root@node1 testgit]# echo 'this is test' >>  readme.txt
[root@node1 testgit]# git add readme.txt
[root@node1 testgit]# 

有任何提示,說明已經添加成功了

第二步:用命令 git commit告訴Git,把文件提交到倉庫。

[root@node1 testgit]#  git commit -m 'readme.txt提交' #-m後面是提交的註釋
[master(根提交) 0cfed5d] readme.txt提交
 1 file changed, 1 insertion(+)
 create mode 100644 readme.txt
[root@node1 testgit]# 

第三步:通過git status來查看是否還有文件未提交的

[root@node1 testgit]# git status
# 位於分支 master
無文件要提交,乾淨的工作區
[root@node1 testgit]# 

說明沒有任何文件未提交,但是我現在繼續來改下readme.txt內容,比如我在下面添加hello world內容,繼續使用git status來查看下結果,如下:

[root@node1 testgit]# echo 'hello world' >> readme.txt 
[root@node1 testgit]# git status
# 位於分支 master
# 尚未暫存以備提交的變更:
#   (使用 "git add <file>..." 更新要提交的內容)
#   (使用 "git checkout -- <file>..." 丟棄工作區的改動)
#
#       修改:      readme.txt
#
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
[root@node1 testgit]# 

上面的命令告訴我們 readme.txt文件已被修改,但是未被提交的修改。

第四步:使用git diff <文件命令>查詢修改何處

[root@node1 testgit]# git diff readme.txt
diff --git a/readme.txt b/readme.txt
index d014168..bfcc640 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1 +1,2 @@
 this is test
+hello world
[root@node1 testgit]# 

如上可以看到,readme.txt文件內容從一行this is test改成二行添加了一行hello world內容。

知道了對readme.txt文件做了什麼修改後,我們可以放心的提交到倉庫了,提交修改和提交文件是一樣的2步(第一步是git add 第二步是:git commit)。

[root@node1 testgit]# git diff readme.txt
diff --git a/readme.txt b/readme.txt
index d014168..bfcc640 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1 +1,2 @@
 this is test
+hello world
[root@node1 testgit]# git add readme.txt
[root@node1 testgit]# git status
# 位於分支 master
# 要提交的變更:
#   (使用 "git reset HEAD <file>..." 撤出暫存區)
#
#       修改:      readme.txt
#
[root@node1 testgit]# git commit -m '文件增加hello world內容'
[master 47c6b31] 文件增加hello world內容
 1 file changed, 1 insertion(+)

版本回退

如上,我們已經學會了修改文件,現在我繼續對readme.txt文件進行修改,再增加一行,內容爲"hello python"。

[root@node1 testgit]# echo "hello python" >> readme.txt 
[root@node1 testgit]# git add readme.txt
[root@node1 testgit]# git commit -m '增加內容hello python'
[master c0a1c2c] 增加內容hello python
 1 file changed, 1 insertion(+)
[root@node1 testgit]# 

現在我已經對readme.txt文件做了三次修改了,那麼我現在想查看下歷史記錄,如何查呢?我們現在可以使用命令 git log 演示如下所示:

[root@node1 testgit]# git log 
commit c0a1c2cf7a67dbd5eba6a8f5ac9c7fcb8b803738 #每次提交的版本號
Author: linjiujiu <[email protected]> #提交的作者 
Date:   Thu Dec 27 17:33:34 2018 +0800 #提交的時間

    增加內容hello python  #提交的註釋

commit 47c6b31839ecc1ebefa8722f36a71b3c9b116daa
Author: linjiujiu <[email protected]>
Date:   Thu Dec 27 15:45:07 2018 +0800

    文件增加hello world內容

commit 0cfed5ddec822bfbea4edd46a16afb560133392d
Author: linjiujiu <[email protected]>
Date:   Thu Dec 27 14:55:40 2018 +0800

    readme.txt提交
[root@node1 testgit]# 

git log命令顯示從最近到最遠的顯示日誌

在我想使用版本回退操作,我想把當前的版本回退到上一個版本,要使用什麼命令呢?可以使用如下2種命令,第一種是:git reset --hard HEAD^那麼如果要回退到上上個版本只需把HEAD^ 改成 HEAD^^ 以此類推。那如果要回退到前100個版本的話,使用上面的方法肯定不方便,我們可以使用下面的簡便命令操作:git reset --hard HEAD~100即可

[root@node1 testgit]# cat readme.txt 
this is test
hello world
hello python
[root@node1 testgit]# git reset --hard HEAD^
HEAD 現在位於 47c6b31 文件增加hello world內容
[root@node1 testgit]# cat readme.txt 
this is test
hello world
[root@node1 testgit]# 

第二種是:git reset --hard 版本號,可以通過如下命令即可獲取到版本號:git reflog

[root@node1 testgit]# git reflog
47c6b31 HEAD@{0}: reset: moving to HEAD^
c0a1c2c HEAD@{1}: commit: 增加內容hello python
47c6b31 HEAD@{2}: commit: 文件增加hello world內容
0cfed5d HEAD@{3}: commit (initial): readme.txt提交
[root@node1 testgit]# 

現在我們想恢復commit: 增加內容hello python.這個版本號,可以通過git reset --hard c0a1c2c

[root@node1 testgit]# cat readme.txt 
this is test
hello world
[root@node1 testgit]# git reset --hard c0a1c2c
HEAD 現在位於 c0a1c2c 增加內容hello python
[root@node1 testgit]# cat readme.txt 
this is test
hello world
hello python
[root@node1 testgit]# 

理解工作區與暫存區的區別

工作區:就是你在電腦上看到的目錄,比如目錄下testgit裏的文件(.git隱藏目錄版本庫除外)。或者以後需要再新建的目錄文件等等都屬於工作區範疇。
版本庫(Repository):工作區有一個隱藏目錄.git,這個不屬於工作區,這是版本庫。其中版本庫裏面存了很多東西,其中最重要的就是stage(暫存區),還有Git爲我們自動創建了第一個分支master,以及指向master的一個指針HEAD。

我們前面說過使用Git提交文件到版本庫有兩步:
第一步:是使用 git add 把文件添加進去,實際上就是把文件添加到暫存區。

第二步:使用git commit提交更改,實際上就是把暫存區的所有內容提交到當前分支上。

Git撤銷修改和刪除文件操作。

撤銷修改:

比如我現在在readme.txt文件裏面增加一行 內容爲"5555555555",在我未提交之前,我發現添加5555555555555內容有誤,所以我得馬上恢復以前的版本,現在我可以有如下幾種方法可以做修改:

第一:如果我知道要刪掉那些內容的話,直接手動更改去掉那些需要的文件,然後add添加到暫存區,最後commit掉。

第二:我可以按以前的方法直接恢復到上一個版本。使用 git reset --hard HEAD^

但是現在我不想使用上面的2種方法,我想直接想使用撤銷命令該如何操作呢?首先在做撤銷之前,我們可以先用 git status 查看下當前的狀態

[root@node1 testgit]# echo "55555"  >> readme.txt 
[root@node1 testgit]# cat readme.txt 
this is test
hello world
hello python
55555
[root@node1 testgit]# git status
# 位於分支 master
# 尚未暫存以備提交的變更:
#   (使用 "git add <file>..." 更新要提交的內容)
#   (使用 "git checkout -- <file>..." 丟棄工作區的改動)
#
#       修改:      readme.txt
#
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
[root@node1 testgit]# 

可以使用git checkout -- file可以丟棄工作區的修改,如下命令:git checkout -- readme.txt,如下所示:

[root@node1 testgit]# git checkout -- readme.txt
[root@node1 testgit]# cat readme.txt 
this is test
hello world
hello python
[root@node1 testgit]# 

命令 git checkout --readme.txt 意思就是,把readme.txt文件在工作區做的修改全部撤銷,這裏有2種情況,如下:
1.readme.txt自動修改後,還沒有放到暫存區,使用 撤銷修改就回到和版本庫一模一樣的狀態。
2.另外一種是readme.txt已經放入暫存區了,接着又作了修改,撤銷修改就回到添加暫存區後的狀態

對於第二種情況,我想我們繼續做demo來看下,假如現在我對readme.txt添加一行 內容爲6666666,我git add 增加到暫存區後,接着添加內容777777,我想通過撤銷命令讓其回到暫存區後的狀態。如下所示:

[root@node1 testgit]# echo "6666666" >> readme.txt 
[root@node1 testgit]# cat readme.txt 
this is test
hello world
hello python
6666666
[root@node1 testgit]# git add readme.txt
[root@node1 testgit]# echo "777777" >> readme.txt 
[root@node1 testgit]# cat readme.txt 
this is test
hello world
hello python
6666666
777777
[root@node1 testgit]# git checkout -- readme.txt
[root@node1 testgit]# cat readme.txt 
this is test
hello world
hello python
6666666
[root@node1 testgit]# 

注意:命令git checkout -- readme.txt 中的 -- 很重要,如果沒有 -- 的話,那麼命令變成創建分支了。

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