Git分佈式版本控制常用命令解析
一、創建版本庫
版本庫(repository)也叫倉庫,可以看做一個目錄,這個目錄裏的所以文件都由Git進行管理,每個文件的修改、刪除,Git都能跟蹤
1、選擇一個合適的地方,創建一個空目錄:
[root@localhost ~]# mkdir villian ##創建一個空目錄作爲本地庫##
[root@localhost ~]# cd villian ##進入到該目錄對該目錄進行初始化##
如果使用Windows系統,要保證目錄名不包含中文
2、通過git init命令把該目錄變成Git可以管理的倉庫:
[root@localhost villian]# git init ##對本地庫進行初始化##
初始化空的 Git 版本庫於 /root/villian/.git/ ##中文解析##
Initialized empty Git repository in /root/villian/.git/ ##英文解析##
二、創建文件、並導入版本庫
1、在villian目錄(工作區)中編寫一個home.txt文件:
vi home.txt
My name is villian
I am from shenzhen
2、使用 git status 命令查看當前狀態
[root@localhost villian]# git status
位於分支 master
初始提交
未跟蹤的文件:
(使用 "git add <文件>..." 以包含要提交的內容)
home.txt
提交爲空,但是存在尚未跟蹤的文件(使用 "git add" 建立跟蹤)
通過git status命令可知,home.txt命令正處於工作區中
3、使用 git add 命令,把文件提交到(暫存區)中:
[root@localhost villian]# git add home.txt
[root@localhost villian]# git status
位於分支 master
初始提交
要提交的變更:
(使用 "git rm --cached <文件>..." 以取消暫存)
新文件: home.txt
通過git status命令可知,home.txt命令正處於倉庫(暫存庫)中
4、1使用 gir rm --cached 命令,把文件從暫存區恢復到工作區中:
[root@localhost villian]# git rm --cached home.txt
rm 'home.txt'
[root@localhost villian]# git status
位於分支 master
初始提交
未跟蹤的文件:
(使用 "git add <文件>..." 以包含要提交的內容)
home.txt
提交爲空,但是存在尚未跟蹤的文件(使用 "git add" 建立跟蹤)
4、2使用 git commit 命令,把文件提交到(本地庫)中:
[root@localhost villian]# git commit -m "home_first" home.txt
位於分支 master
無文件要提交,乾淨的工作區
[root@localhost villian]# git status
位於分支 master
無文件要提交,乾淨的工作區
三、修改文件
我們已經成功添加並提交了一個 home.txt 文件,現繼續修改 readme.txt 文件
1、向文本中添加一行“I am from Guangdong”信息
[root@localhost villian]# cat home.txt
My name is villian
I am from shenzhen
I am from Guangdong ##新增一行信息##
2、使用 git diff 命令查看本次修改變動內容
[root@localhost villian]# git diff home.txt
diff --git a/home.txt b/home.txt
index 8c8045d..f3ab093 100644
--- a/home.txt
+++ b/home.txt
@@ -1,2 +1,3 @@
My name is villian
I am from shenzhen
+I am from Guangdong ##此處新增一行##
3、使用 git status 命令查看當前狀態
[root@localhost villian]# git status
位於分支 master
尚未暫存以備提交的變更:
(使用 "git add <文件>..." 更新要提交的內容)
(使用 "git checkout -- <文件>..." 丟棄工作區的改動)
修改: home.txt
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
此次狀態與新建文件時狀態類似,但是多了一行撤銷(丟棄)修改的提示
4、1使用 git chekout 命令,將本次修改撤銷
[root@localhost villian]# git checkout home.txt
[root@localhost villian]# git status
位於分支 master
無文件要提交,乾淨的工作區
使用git chekout 命令後,恢復到了修改之前的狀態
通過cat命令查看home.txt文本內容(此時 I am from Guangdong已經不見)
[root@localhost villian]# cat home.txt
My name is villian
I an from shenzhen
4、2使用git add命令將修改後內容提交到暫存庫中
[root@localhost villian]# git status ##接上述第2步修改內容##
位於分支 master
尚未暫存以備提交的變更:
(使用 "git add <文件>..." 更新要提交的內容)
(使用 "git checkout -- <文件>..." 丟棄工作區的改動)
修改: home.txt
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
[root@localhost villian]# git add home.txt ##重新提交至暫存區##
[root@localhost villian]# git status
位於分支 master
要提交的變更:
(使用 "git reset HEAD <文件>..." 以取消暫存)
修改: home.txt
5、使用git commit -m 命令將home.txt從暫存區提交到本地庫中
[root@localhost villian]# git commit -m "home_next" home.txt
位於分支 master
無文件要提交,乾淨的工作區
[root@localhost villian]# git status
位於分支 master
無文件要提交,乾淨的工作區
四、版本回退
爲增加試驗效果,此時我已經對home.txt文本修改了5次
1、1使用 git log 查看修改記錄
注:一大串數字是 commit id
[root@localhost villian]# git log
commit 7d35d6840844b0e6c96d7d7209ff9f45a1029315
Author: root <root@localhost.localdomain>
Date: Sat Oct 26 16:50:30 2019 +0800
home_fifth
commit 7a690b414f87fbbe746338d247260021a51a43e2
Author: root <root@localhost.localdomain>
Date: Sat Oct 26 16:49:16 2019 +0800
home_fourth
commit f9828f5273b60638b9f67f135cd4d4b74f3ddb9f
Author: root <root@localhost.localdomain>
Date: Sat Oct 26 16:46:55 2019 +0800
home_third
commit 2afde91ff027301499b77cd940b651dda18d5bae
Author: root <root@localhost.localdomain>
Date: Sat Oct 26 16:38:07 2019 +0800
home_next
commit 9e6906339361841d1c8b27a4cca91ddc4dba0b99
Author: root <root@localhost.localdomain>
Date: Sat Oct 26 16:04:23 2019 +0800
home_first
1、2 使用–pretty=oneline 參數,單行顯示修改記錄
[root@localhost villian]# git log --pretty=oneline
7d35d6840844b0e6c96d7d7209ff9f45a1029315 home_fifth
7a690b414f87fbbe746338d247260021a51a43e2 home_fourth
f9828f5273b60638b9f67f135cd4d4b74f3ddb9f home_third
2afde91ff027301499b77cd940b651dda18d5bae home_next
9e6906339361841d1c8b27a4cca91ddc4dba0b99 home_first
1、3使用 --oneline 參數,更爲簡潔顯示修改記錄
[root@localhost villian]# git log --oneline
7d35d68 home_fifth
7a690b4 home_fourth
f9828f5 home_third
2afde91 home_next
9e69063 home_first
2、使用 git reset --hard 命令修改版本
[root@localhost villian]# git reflog
7d35d68 HEAD@{0}: commit: home_fifth
7a690b4 HEAD@{1}: commit: home_fourth
f9828f5 HEAD@{2}: commit: home_third
2afde91 HEAD@{3}: commit: home_next
9e69063 HEAD@{4}: commit (initial): home_first
[root@localhost villian]# git reset --hard 7a690b4 ##恢復到第四版本##
HEAD 現在位於 7a690b4 home_fourth
## 使用cat home.txt查看文本內容是否已變化 ##
[root@localhost villian]# cat home.txt
My name is villian
I am from shenzhen
I am from Guangdong
I am from China
I am from Asia
最後一個 “I am from the earth”已經不見
五、工作區和暫存區
-
工作區(Working Directory)
learngit 文件夾就是一個工作區。 -
版本庫(Repository)
工作區有個隱藏目錄 .git ,這個不算工作區,而是 Git 的版本庫。 -
版本庫裏面的 index(stage) 文件叫暫存區,還有Git爲我們自動創建的第一個分支 master ,以及指向 master 的一個指針叫做 HEAD。
前面我們提到過,如果我們想把文件添加到Git裏面時,需要分兩步:
-
第一步是用 git add 把文件添加進去,實際上就是把文件修改添加到暫存區。
-
第二步是用 git commit 提交更改,實際上就是把暫存區的所有內容提交到當前分支。(我們現在只有唯一一個分支 master,所以現在就是往 master 分支上提交更改)
我們可以實踐一下:
- 1、在 readme.txt 文件中加上一行內容:
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
- 2、然後在工作區新建一個 LICENSE 文本文檔(任意內容)
使用兩次 git add 命令分別把 readme.txt 和 LICENSE 都添加後,可以用 git status 命令查看一下:
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: LICENSE
modified: readme.txt
- 3、現在,暫存區的狀態就變成這樣了:
- 4、再使用 git commit 命令把暫存區的所有修改提交到分支:
$ git commit -m "understand how stage works"
[master e43a48b] understand how stage works
2 files changed, 2 insertions(+)
create mode 100644 LICENSE
- 5、這時候的工作區就是乾淨的:
$ git status
On branch master
nothing to commit, working tree clean
六、撤銷修改
假如說你在 home.txt 文件中添加了一行內容如下:
[root@localhost villian]# cat home.txt
My name is villian
I am from shenzhen
I am from Guangdong
I am from China
I am from Asia
Shenzhen is a very beautiful city
想要刪除,應該怎麼撤銷呢?
1、沒有 git add 之前
- 通過手動進入Vim編輯器刪除最後一行,手動把文件恢復到上一個版本的狀態。
- 然後再使用 git checkout file 命令丟棄工作區的修改:
$ git checkout home.txt //把home.txt文件在工作區的修改全部撤銷。
- 現在看一下 readme.txt 文件內容:
[root@localhost villian]# cat home.txt
My name is villian
I am from shenzhen
I am from Guangdong
I am from China
I am from Asia
[root@localhost villian]# git status
位於分支 master
無文件要提交,乾淨的工作區
2、使用 git add 提交到暫存區,但沒有使用 git commit 提交到版本庫中
- 這時候的修改添加到了暫存區,但沒有提交到分支,用 git status 查看一下:
[root@localhost villian]# git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: readme.txt
- 這時候我們可以使用 git reset HEAD file 命令把把暫存區的修改撤銷掉,重新放回工作區:
[root@localhost villian]# git reset HEAD home.txt //git reset命令既可以回退版本,也可以把暫存區的修改回退到工作區,HEAD表示最新版本。
Unstaged changes after reset:
M readme.txt
- 現在再用 git status 查看一下:
[root@localhost villian]# 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: readme.txt //暫存區是乾淨的,工作區有修改
- 這時候再丟棄工作區的修改就OK了:
[root@localhost villian]# git checkout home.txt //丟棄工作區的修改
[root@localhost villian]# git status
On branch master
nothing to commit, working tree clean
3、 既使用 git add 提交到了暫存區,也使用 git commit 提交到了版本庫
- 使用 git reset 命令回退版本
七、刪除文件
- 在工作區即 villian 目錄下新建一個 test.txt 文件,並添加和提交到Git:
[root@localhost villian]# git add test.txt
[root@localhost villian]# git commit -m "add test.txt"
[master b84166e] add test.txt
1 file changed, 1 insertion(+)
create mode 100644 test.txt
- 這時候可用 rm 命令刪除:
[root@localhost villian]# rm test.txt
這時工作區和版本庫就不一樣了
現在又分兩種情況:
1、 確實要從版本庫中刪除該文件,那就用 git rm 命令刪除,並且 git commit:
[root@localhost villian]# git rm test.txt
rm 'test.txt'
[root@localhost villian]# git commit -m "remove test.txt"
[root@localhost villian]# remove test.txt
1 file changed, 1 deletion(-)
delete mode 100644 test.txt
- 這時候文件就從版本庫被刪除了。
2、文件被刪錯了。因爲版本庫裏有,所以很好恢復:
[root@localhost villian]# git checkout test.txt //用版本庫裏的版本替換工作區的版本。
八、比較文件
比較誰跟誰的區別,就將該文件提交至該區域,然後再修改文件提交至比較區域
例如:暫存區與版本庫的區別,就先將文本提交至版本庫中,然後再修改該文件,提交至暫存區,最後通過相應的命令(git diff --cached id)進行對比;
- 創建zwl.txt文本,內容如下:
[root@localhost villian]# cat zwl.txt
my name is zwl
- 然後將該文本提交至暫存區中
[root@localhost villian]# git add zwl.txt
1、比較工作區與暫存區的區別
- 修改工作區中zwl.txt文本內容,向文本中新增“i am from shenhen”
[root@localhost villian]# cat zwl.txt
my name is zwl
i am from shenzhen
- 通過 git diff 命令進行對比
[root@localhost villian]# git diff zwl.txt
diff --git a/zwl.txt b/zwl.txt
index fbbccbf..8cb4939 100644
--- a/zwl.txt
+++ b/zwl.txt
@@ -1 +1,2 @@
my name is zwl ///此處爲新增內容
+i am from shenzhen
2、比較工作區與版本庫的區別
- 將zwl.txt從暫存區提交至版本庫中
[root@localhost villian]# git commit -m "zwl" zwl.txt
- 修改工作區zwl.txt文本內容,新增“hello”
[root@localhost villian]# cat zwl.txt
my name is zwl
i am from shenzhen
hello
- 通過git log --oneline 命令查看commit_id號
[root@localhost villian]# git log --oneline
93b356b zwl1
484c46a zwl
通過下述命令對比工作區和版本庫中的區別
- git diff 484c
- git diff HEAD // 如果是與最新的提交比較,commit id可以使用 HEAD代替
[root@localhost villian]# git diff 484c
diff --git a/zwl.txt b/zwl.txt
index 8cb4939..d624612 100644
--- a/zwl.txt
+++ b/zwl.txt
@@ -1,2 +1,4 @@
my name is zwl
i am from shenzhen
+i am 18
+hello
3、比較暫存區與版本庫文件的區別
- 修改版本庫中的zwl.txt文本內容,新增“2019”
[root@localhost villian]# cat zwl.txt
my name is zwl
i am from shenzhen
i am 18
hello
2019
- 將zwl.txt從工作區提交至暫存區
[root@localhost villian]# git add zwl.txt
通過下述命令對比暫存區和版本庫中的區別
- git diff --cached 484c
- git diff–cached HEAD // 如果是與最新的提交比較,commit id可以使用 HEAD代替
[root@localhost villian]# git diff --cached HEAD
diff --git a/zwl.txt b/zwl.txt
index 71f8256..aef3448 100644
--- a/zwl.txt
+++ b/zwl.txt
@@ -1,3 +1,5 @@
my name is zwl
i am from shenzhen
i am 18
+hello
+2019