【文章內(nèi)容簡(jiǎn)介】
第一個(gè)分支master,以及指向master的一個(gè)指針叫HEAD。分支和HEAD的概念我們以后再講。前面講了我們把文件往Git版本庫(kù)里添加的時(shí)候,是分兩步執(zhí)行的:第一步是用git add把文件添加進(jìn)去,實(shí)際上就是把文件修改添加到暫存區(qū);第二步是用git mit提交更改,實(shí)際上就是把暫存區(qū)的所有內(nèi)容提交到當(dāng)前分支。因?yàn)槲覀儎?chuàng)建Git版本庫(kù)時(shí),Git自動(dòng)為我們創(chuàng)建了唯一一個(gè)master分支,所以,現(xiàn)在,git mit就是往master分支上提交更改。你可以簡(jiǎn)單理解為,需要提交的文件修改通通放到暫存區(qū),然后,一次性提交暫存區(qū)的所有修改。俗話說,實(shí)踐出真知?,F(xiàn)在,我們?cè)倬毩?xí)一遍,比如加上一行內(nèi)容:Git is a distributed version control system.Git is free software distributed under the GPL.Git has a mutable index called stage.然后,在工作區(qū)新增一個(gè)LICENSE文本文件(內(nèi)容隨便寫)。先用git status查看一下狀態(tài):$ git status On branch master Changes not staged for mit: (use git add file... to update what will be mitted) (use git checkout file... to discard changes in working directory) modified: Untracked files: (use git add file... to include in what will be mitted) LICENSEno changes added to mit (use git add and/or git mit a)Git非常清楚地告訴我們,而LICENSE還從來沒有被添加過,所以它的狀態(tài)是Untracked?,F(xiàn)在,使用兩次命令git add,用git status再查看一下:$ git status On branch master Changes to be mitted: (use git reset HEAD file... to unstage) new file: LICENSE modified: 現(xiàn)在,暫存區(qū)的狀態(tài)就變成這樣了:所以,git add命令實(shí)際上就是把要提交的所有修改放到暫存區(qū)(Stage),然后,執(zhí)行g(shù)it mit就可以一次性把暫存區(qū)的所有修改提交到分支。$ git mit m understand how stage works[master 27c9860] understand how stage works 2 files changed, 675 insertions(+) create mode 100644 LICENSE一旦提交后,如果你又沒有對(duì)工作區(qū)做任何修改,那么工作區(qū)就是“干凈”的:$ git status On branch masternothing to mit (working directory clean)現(xiàn)在版本庫(kù)變成了這樣,暫存區(qū)就沒有任何內(nèi)容了:小結(jié)暫存區(qū)是Git非常重要的概念,弄明白了暫存區(qū),就弄明白了Git的很多操作到底干了什么。沒弄明白暫存區(qū)是怎么回事的童鞋,請(qǐng)向上滾動(dòng)頁(yè)面,再看一次。管理修改閱讀: 474813現(xiàn)在,假定你已經(jīng)完全掌握了暫存區(qū)的概念。下面,我們要討論的就是,為什么Git比其他版本控制系統(tǒng)設(shè)計(jì)得優(yōu)秀,因?yàn)镚it跟蹤并管理的是修改,而非文件。你會(huì)問,什么是修改?比如你新增了一行,這就是一個(gè)修改,刪除了一行,也是一個(gè)修改,更改了某些字符,也是一個(gè)修改,刪了一些又加了一些,也是一個(gè)修改,甚至創(chuàng)建一個(gè)新文件,也算一個(gè)修改。為什么說Git管理的是修改,而不是文件呢?我們還是做實(shí)驗(yàn)。第一步,比如加一行內(nèi)容:$ cat Git is a distributed version control system.Git is free software distributed under the GPL.Git has a mutable index called stage.Git tracks changes.然后,添加:$ git add $ git status On branch master Changes to be mitted: (use git reset HEAD file... to unstage) modified: 然后,:$ cat Git is a distributed version control system.Git is free software distributed under the GPL.Git has a mutable index called stage.Git tracks changes of files.提交:$ git mit m git tracks changes[master d4f25b6] git tracks changes 1 file changed, 1 insertion(+)提交后,再看看狀態(tài):$ git status On branch master Changes not staged for mit: (use git add file... to update what will be mitted) (use git checkout file... to discard changes in working directory) modified: no changes added to mit (use git add and/or git mit a)咦,怎么第二次的修改沒有被提交?別激動(dòng),我們回顧一下操作過程:第一次修改 git add 第二次修改 git mit你看,我們前面講了,Git管理的是修改,當(dāng)你用git add命令后,在工作區(qū)的第一次修改被放入暫存區(qū),準(zhǔn)備提交,但是,在工作區(qū)的第二次修改并沒有放入暫存區(qū),所以,git mit只負(fù)責(zé)把暫存區(qū)的修改提交了,也就是第一次的修改被提交了,第二次的修改不會(huì)被提交。提交后,用git diff HEAD :$ git diff HEAD diff git a/ b/index 76d770f..a9c5755 100644 a/+++ b/@@ 1,4 +1,4 @@ Git is a distributed version control system. Git is free software distributed under the GPL. Git has a mutable index called stage.Git tracks changes.+Git tracks changes of files.可見,第二次修改確實(shí)沒有被提交。那怎么提交第二次修改呢?你可以繼續(xù)git add再git mit,也可以別著急提交第一次修改,先git add第二次修改,再git mit,就相當(dāng)于把兩次修改合并后一塊提交了:第一次修改 git add 第二次修改 git add git mit好,現(xiàn)在,把第二次修改提交了,然后開始小結(jié)。小結(jié)現(xiàn)在,你又理解了Git是如何跟蹤修改的,每次修改,如果不add到暫存區(qū),那就不會(huì)加入到mit中。撤銷修改閱讀: 463113自然,你是不會(huì)犯錯(cuò)的。不過現(xiàn)在是凌晨?jī)牲c(diǎn),你正在趕一份工作報(bào)告,:$ cat Git is a distributed version control system.Git is free software distributed under the GPL.Git has a mutable index called stage.Git tracks changes of files.My stupid boss still prefers SVN.在你準(zhǔn)備提交前,一杯咖啡起了作用,你猛然發(fā)現(xiàn)了“stupid boss”可能會(huì)讓你丟掉這個(gè)月的獎(jiǎng)金!既然錯(cuò)誤發(fā)現(xiàn)得很及時(shí),就可以很容易地糾正它。你可以刪掉最后一行,手動(dòng)把文件恢復(fù)到上一個(gè)版本的狀態(tài)。如果用git status查看一下:$ git status On branch master Changes not staged for mit: (use git add file... to update what will be mitted) (use git checkout file... to discard changes in working directory) modified: no changes added to mit (use git add and/or git mit a)你可以發(fā)現(xiàn),Git會(huì)告訴你,git checkout file可以丟棄工作區(qū)的修改:$ git checkout 命令git checkout ,這里有兩種情況:,現(xiàn)在,撤銷修改就回到和版本庫(kù)一模一樣的狀態(tài);,又作了修改,現(xiàn)在,撤銷修改就回到添加到暫存區(qū)后的狀態(tài)??傊褪亲屵@個(gè)文件回到最近一次git mit或git add時(shí)的狀態(tài)?,F(xiàn)在,:$ cat Git is a distributed version control system.Git is free software distributed under the GPL.Git has a mutable index called stage.Git tracks changes of files.文件內(nèi)容果然復(fù)原了。git checkout file命令中的很重要,沒有,就變成了“切換到另一個(gè)分支”的命令,我們?cè)诤竺娴姆种Ч芾碇袝?huì)再次遇到git checkout命令?,F(xiàn)在假定是凌晨3點(diǎn),你不但寫了一些胡話,還git add到暫存區(qū)了:$ cat Git is a distributed version control system.Git is free software distributed under the GPL.Git has a mutable index called stage.Git tracks changes of files.My stupid boss still prefers SVN.$ git add 慶幸的是,在mit之前,你發(fā)現(xiàn)了這個(gè)問題。用git status查看一下,修改只是添加到了暫存區(qū),還沒有提交:$ git status On branch master Changes to be mitted: (use git reset HEAD file... to unstage) modified: Git同樣告訴我們,用命令git reset HEAD file可以把暫存區(qū)的修改撤銷掉(unstage),重新放回工作區(qū):$ git reset HEAD Unstaged changes after reset:M git reset命令既可以回退版本,也可以把暫存區(qū)的修改回退到工作區(qū)。當(dāng)我們用HEAD時(shí),表示最新的版本。再用git status查看一下,現(xiàn)在暫存區(qū)是干凈的,工作區(qū)有修改:$ git status On branch master Changes not staged for mit: (use git add file... to update what will be mitted) (use git checkout file... to discard changes in working directory) modified: no changes added to mit (use git add and/or git mit a)還記得如何丟棄工作區(qū)的修改嗎?$ git checkout $ git status On branch masternothing to mit (working directory clean)整個(gè)世界終于清靜了!現(xiàn)在,假設(shè)你不但改錯(cuò)了東西,還從暫存區(qū)提交到了版本庫(kù),怎么辦呢?還記得版本回退一節(jié)嗎?可以回退到上一個(gè)版本。不過,這是有條件的,就是你還沒有把自己的本地版本庫(kù)推送到遠(yuǎn)程。還記得Git是分布式版本控制系統(tǒng)嗎?我們后面會(huì)講到遠(yuǎn)程版本庫(kù),一旦你把“stupid boss”提交推送到遠(yuǎn)程版本庫(kù),你就真的慘了……小結(jié)又到了小結(jié)時(shí)間。場(chǎng)景1:當(dāng)你改亂了工作區(qū)某個(gè)文件的內(nèi)容,想直接丟棄工作區(qū)的修改時(shí),用命令git checkout file。場(chǎng)景2:當(dāng)你不但改亂了工作區(qū)某個(gè)文件的內(nèi)容,還添加到了暫存區(qū)時(shí),想丟棄修改,分兩步,第一步用命令git reset HEAD file,就回到了場(chǎng)景1,第二步按場(chǎng)景1操作。場(chǎng)景3:已經(jīng)提交了不合適的修改到版