git 工作原理圖
如上圖所示垢啼,有三個區(qū)域Working Directory、stage张肾、master芭析。
名詞解釋:
工作區(qū)(Working Directory)
在我們直接編輯文件(文件夾)的根目錄,如下圖:
在G盤Git目錄下就是工作區(qū)
版本庫(Repository)
版本庫才是git正式工作的地方吞瞪,在工作區(qū)下隱藏目錄里馁启,如下圖:
版本庫主要包括兩個區(qū),如上圖芍秆,包括“stage”和“master”惯疙。
- master
master區(qū)管理了我們每次提交后的文件版本以及相關(guān)信息,是git最重要的倉庫妖啥。在master區(qū)霉颠,有一個head指針(見圖1),指向最新提交的版本荆虱。 - stage
stage是工作區(qū)到master區(qū)的緩存區(qū)掉分,在做小的修改時我們可以先提交到stage俭缓,確定沒有問題了,或者當(dāng)天的工作完成了酥郭,再把緩存區(qū)的內(nèi)容最終提交到master。
工作原理
上篇文章里講了命令行:git add愿吹、git commit
其中執(zhí)行g(shù)it add的時候不从,是把在“Working Directory”去修改(增加或刪除)的內(nèi)容提交到“stage”
執(zhí)行g(shù)it commit之類的時候,把“stage”的內(nèi)容最終提交到“master”區(qū)犁跪。
git status
命令行g(shù)it status 能夠幫助我們快速的了解git的當(dāng)前狀態(tài)椿息。
例如,我們現(xiàn)在工作區(qū)新增一個test3.txt文件(先不要添加到版本庫)坷衍,然后執(zhí)行g(shù)it status寝优。
chenxi@chenxi_pc MINGW64 /G/Git (master)
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
test3.txt
nothing added to commit but untracked files present (use "git add" to track)
從上面的代碼我們可以看出,有在工作區(qū)新文件(test3.txt)待提交枫耳,但從stage區(qū)到master區(qū)沒有新內(nèi)容提交乏矾。
所以根據(jù)上篇文章的知識,先用git add把test3.txt提交到stage迁杨。執(zhí)行指令git add后钻心,再使用git status查當(dāng)前git狀態(tài),如下代碼:
chenxi@chenxi_pc MINGW64 /G/Git (master)
$ git add test3.txt
chenxi@chenxi_pc MINGW64 /G/Git (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: test3.txt
chenxi@chenxi_pc MINGW64 /G/Git (master)
從上面的的結(jié)果知道已經(jīng)成功添加到status铅协,準備好等待我們提交到master捷沸,執(zhí)行g(shù)it commit可以完成提交任務(wù),如下圖:
chenxi@chenxi_pc MINGW64 /G/Git (master)
$ git commit -m "add test3.txt file"
[master 39e0dba] add test3.txt file
1 file changed, 1 insertion(+)
create mode 100644 test3.txt
chenxi@chenxi_pc MINGW64 /G/Git (master)
chenxi@chenxi_pc MINGW64 /G/Git (master)
$ git status
On branch master
nothing to commit, working directory clean
chenxi@chenxi_pc MINGW64 /G/Git (master)
版本回退
通過前面我們已經(jīng)掌握了提交新版本狐史,如果發(fā)現(xiàn)我修改錯了東西痒给,怎樣返回新版本呢?
我們修改test.txt文件骏全,添加新內(nèi)容苍柏,如下:
執(zhí)行g(shù)it add、git commit指令吟温,如以下代碼:
chenxi@chenxi_pc MINGW64 /G/Git (master)
$ 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")
chenxi@chenxi_pc MINGW64 /G/Git (master)
$ git add test.txt
chenxi@chenxi_pc MINGW64 /G/Git (master)
$ git commit -m "add hello world to test.txt"
[master 78db795] add hello world to test.txt
1 file changed, 6 insertions(+), 1 deletion(-)
chenxi@chenxi_pc MINGW64 /G/Git (master)
再在上面的基礎(chǔ)上添加新內(nèi)容序仙,如下圖:
并提交:
chenxi@chenxi_pc MINGW64 /G/Git (master)
$ git add test.txt
chenxi@chenxi_pc MINGW64 /G/Git (master)
$ git commit -m "add hello CSDN to test.txt"
[master 8f4ae6d] add hello CSDN to test.txt
1 file changed, 2 insertions(+)
chenxi@chenxi_pc MINGW64 /G/Git (master)
好,現(xiàn)在我們使用log指令查一下我們都執(zhí)行了哪些操作:
chenxi@chenxi_pc MINGW64 /G/Git (master)
$ git log
commit 8f4ae6dc10a67a0a5726f449c6c65e288d39f57f
Author: chenximcm <1178898205@qq.com>
Date: Fri Jan 13 20:23:18 2017 +0800
add hello CSDN to test.txt
commit 78db795dabdeb752d635ed2960b87ec49bb13943
Author: chenximcm <1178898205@qq.com>
Date: Fri Jan 13 20:15:46 2017 +0800
add hello world to test.txt
commit 39e0dba93090a1f01887180e7bedbb7dda1fe5b3
Author: chenximcm <1178898205@qq.com>
Date: Fri Jan 13 19:36:40 2017 +0800
add test3.txt file
commit 63d890ed08ee938dc4e83ad14446727dc9d55da3
Author: chenximcm <1178898205@qq.com>
Date: Fri Dec 30 17:43:55 2016 +0800
add test1.txt and test2.txt files
commit 489b113d392d1bc930f6eef1b6f0135d6bd6e0d3
Author: chenximcm <1178898205@qq.com>
Date: Fri Dec 30 12:13:05 2016 +0800
add panoramaProjects folder
:...skipping...
commit 8f4ae6dc10a67a0a5726f449c6c65e288d39f57f
Author: chenximcm <1178898205@qq.com>
Date: Fri Jan 13 20:23:18 2017 +0800
add hello CSDN to test.txt
commit 78db795dabdeb752d635ed2960b87ec49bb13943
Author: chenximcm <1178898205@qq.com>
Date: Fri Jan 13 20:15:46 2017 +0800
add hello world to test.txt
commit 39e0dba93090a1f01887180e7bedbb7dda1fe5b3
Author: chenximcm <1178898205@qq.com>
Date: Fri Jan 13 19:36:40 2017 +0800
add test3.txt file
commit 63d890ed08ee938dc4e83ad14446727dc9d55da3
Author: chenximcm <1178898205@qq.com>
Date: Fri Dec 30 17:43:55 2016 +0800
add test1.txt and test2.txt files
commit 489b113d392d1bc930f6eef1b6f0135d6bd6e0d3
Author: chenximcm <1178898205@qq.com>
Date: Fri Dec 30 12:13:05 2016 +0800
add panoramaProjects folder
commit 664376a8783c533c90731dd00e4093d8bff9e97b
Author: chenximcm <1178898205@qq.com>
Date: Fri Dec 30 12:10:09 2016 +0800
add test.txt file
~
~
chenxi@chenxi_pc MINGW64 /G/Git (master)
好多啊鲁豪,是不是覺得輸出太多信息了潘悼,沒事,我們可以把每次的信息只輸出精簡信息爬橡,我們只需要在git log 后面添加參數(shù)“–pretty=oneline”即可治唤。
chenxi@chenxi_pc MINGW64 /G/Git (master)
$ git log --pretty=oneline
8f4ae6dc10a67a0a5726f449c6c65e288d39f57f add hello CSDN to test.txt
78db795dabdeb752d635ed2960b87ec49bb13943 add hello world to test.txt
39e0dba93090a1f01887180e7bedbb7dda1fe5b3 add test3.txt file
63d890ed08ee938dc4e83ad14446727dc9d55da3 add test1.txt and test2.txt files
489b113d392d1bc930f6eef1b6f0135d6bd6e0d3 add panoramaProjects folder
664376a8783c533c90731dd00e4093d8bff9e97b add test.txt file
chenxi@chenxi_pc MINGW64 /G/Git (master)
是不是把目前不需要的信息都省掉了?每條信息只給出版本號跟我們添加的日志描述信息糙申。如果你覺得最后一次操作有問題(添加的hello CSDN)宾添,想返回到前面的按個版本,怎么辦?
只需要執(zhí)行指令:“git reset –hard HEAD^”
HEAD前面說過有個HEAD指針指向最新提交的版本缕陕,HEAD表示當(dāng)前版本的前面那個版本粱锐,HEAD表示前前個版本,如果想返回前面第100個版本扛邑,是不是可以寫100個“”怜浅。
O(∩_∩)O哈哈~,讓我想起了以前小時候老師講的一個故事蔬崩,一個小學(xué)生學(xué)習(xí)了中文的一恶座、二、三沥阳,他就說后面的他都會了跨琳,就沒去上課,回家他老爸讓他寫一個萬字桐罕,他寫了一個下午脉让。(小插曲)
當(dāng)然上面的方法也可以,不過我們都沒那么蠢吧冈绊。我們可以使用HEAD~100代替侠鳄。
如上圖,我們執(zhí)行了git reset –hard HEAD^后死宣,是不是返回到前面那個版本了“78db795dabdeb752d635ed2960b87ec49bb13943 add hello world to test.txt
”
再去查看一下我們的test.txt文件是不是也回到前面那個版本伟恶,如下圖:
慘了,我們前面那個版本了(add hello CSDN…)毅该,怎么辦博秫?
不慌不慌,我們還可以通過前面的版本ID號返回去呢眶掌。版本號辣么長挡育,我要暈了…放心,git不會這么麻煩的朴爬,你只需要輸入其中連續(xù)的幾個版本號字符就可以了(一般6位)即寒,這樣git就能辨別了。
如下圖:
又回來了召噩,開不開心母赵?O(∩_∩)O哈哈~!當(dāng)然你也可以通過版本號返回到其他的版本具滴。
在master去凹嘲,有個HEAD指針,該指針指向當(dāng)前的版本构韵,返回到第n個版本就是通過修改HEAD指針的值實現(xiàn)周蹭,如上圖趋艘。
小結(jié)
- git版本庫主要的三個工作區(qū)域工作區(qū)、stage凶朗、master瓷胧。
- git status查詢當(dāng)前狀態(tài)
- 通過git reset –hard HEAD^返回以前版本的信息
- 通過git reset –hard 版本號返回版本號對應(yīng)的版本