git修改和刪除功能_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
為什么Git比其他版本控制系統(tǒng)設(shè)計(jì)得優(yōu)秀,因?yàn)镚it跟蹤并管理的是修改,而非文件。
你會(huì)問,什么是修改?比如你新增了一行,這就是一個(gè)修改,刪除了一行,也是一個(gè)修改,更改了某些字符,也是一個(gè)修改,刪了一些又加了一些,也是一個(gè)修改,甚至創(chuàng)建一個(gè)新文件,也算一個(gè)修改。
為什么說Git管理的是修改,而不是文件呢?我們還是做實(shí)驗(yàn)。第一步,對(duì)readme.txt做一個(gè)修改,比如加一行內(nèi)容:
$ cat readme.txt 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 readme.txt $ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: readme.txt #
然后,再修改readme.txt:
$ cat readme.txt 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 commit -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 commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: readme.txt # no changes added to commit (use "git add" and/or "git commit -a")
咦,怎么第二次的修改沒有被提交?
別激動(dòng),我們回顧一下操作過程:
第一次修改 -> git add -> 第二次修改 -> git commit
你看,我們前面講了,Git管理的是修改,當(dāng)你用git add命令后,在工作區(qū)的第一次修改被放入暫存區(qū),準(zhǔn)備提交,但是,在工作區(qū)的第二次修改并沒有放入暫存區(qū),所以,git commit只負(fù)責(zé)把暫存區(qū)的修改提交了,也就是第一次的修改被提交了,第二次的修改不會(huì)被提交。
提交后,用git diff HEAD -- readme.txt命令可以查看工作區(qū)和版本庫(kù)里面最新版本的區(qū)別:
$ git diff HEAD -- readme.txt diff --git a/readme.txt b/readme.txt index 76d770f..a9c5755 100644 --- a/readme.txt +++ b/readme.txt @@ -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 commit,也可以別著急提交第一次修改,先git add第二次修改,再git commit,就相當(dāng)于把兩次修改合并后一塊提交了:
第一次修改 -> git add -> 第二次修改 -> git add -> git commit
好,現(xiàn)在,把第二次修改提交了,然后開始小結(jié)。
自然,你是不會(huì)犯錯(cuò)的。不過現(xiàn)在是凌晨?jī)牲c(diǎn),你正在趕一份工作報(bào)告,你在readme.txt中添加了一行:
$ cat readme.txt 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 commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: readme.txt # no changes added to commit (use "git add" and/or "git commit -a")
你可以發(fā)現(xiàn),Git會(huì)告訴你,git checkout -- file可以丟棄工作區(qū)的修改:
$ git checkout -- readme.txt
命令git checkout -- readme.txt意思就是,把readme.txt文件在工作區(qū)的修改全部撤銷,這里有兩種情況:
一種是readme.txt自修改后還沒有被放到暫存區(qū),現(xiàn)在,撤銷修改就回到和版本庫(kù)一模一樣的狀態(tài);
一種是readme.txt已經(jīng)添加到暫存區(qū)后,又作了修改,現(xiàn)在,撤銷修改就回到添加到暫存區(qū)后的狀態(tài)。
總之,就是讓這個(gè)文件回到最近一次git commit或git add時(shí)的狀態(tài)。
現(xiàn)在,看看readme.txt的文件內(nèi)容:
$ cat readme.txt 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命令。
現(xiàn)在假定是凌晨3點(diǎn),你不但寫了一些胡話,還git add到暫存區(qū)了:
$ cat readme.txt 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 readme.txt
慶幸的是,在commit之前,你發(fā)現(xiàn)了這個(gè)問題。用git status查看一下,修改只是添加到了暫存區(qū),還沒有提交:
$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: readme.txt #
Git同樣告訴我們,用命令git reset HEAD file可以把暫存區(qū)的修改撤銷掉(unstage),重新放回工作區(qū):
$ git reset HEAD readme.txt Unstaged changes after reset: M readme.txt
git reset命令既可以回退版本,也可以把暫存區(qū)的修改回退到工作區(qū)。當(dāng)我們用HEAD時(shí),表示最新的版本。
再用git status查看一下,現(xiàn)在暫存區(qū)是干凈的,工作區(qū)有修改:
$ 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 # no changes added to commit (use "git add" and/or "git commit -a")
還記得如何丟棄工作區(qū)的修改嗎?
$ git checkout -- readme.txt $ git status # On branch master nothing to commit (working directory clean)
整個(gè)世界終于清靜了!
現(xiàn)在,假設(shè)你不但改錯(cuò)了東西,還從暫存區(qū)提交到了版本庫(kù),怎么辦呢?git可以回退到上一個(gè)版本。不過,這是有條件的,就是你還沒有把自己的本地版本庫(kù)推送到遠(yuǎn)程。一旦你把“stupid boss”提交推送到遠(yuǎn)程版本庫(kù),你就真的慘了
在Git中,刪除也是一個(gè)修改操作,我們實(shí)戰(zhàn)一下,先添加一個(gè)新文件test.txt到Git并且提交:
$ git add test.txt $ git commit -m "add test.txt" [master 94cdc44] add test.txt 1 file changed, 1 insertion(+) create mode 100644 test.txt
一般情況下,你通常直接在文件管理器中把沒用的文件刪了,或者用rm命令刪了:
$ rm test.txt
這個(gè)時(shí)候,Git知道你刪除了文件,因此,工作區(qū)和版本庫(kù)就不一致了,git status命令會(huì)立刻告訴你哪些文件被刪除了:
$ 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 # no changes added to commit (use "git add" and/or "git commit -a")
現(xiàn)在你有兩個(gè)選擇,一是確實(shí)要從版本庫(kù)中刪除該文件,那就用命令git rm刪掉,并且git commit:
$ git rm test.txt rm 'test.txt' $ git commit -m "remove test.txt" [master d17efd8] remove test.txt 1 file changed, 1 deletion(-) delete mode 100644 test.txt
現(xiàn)在,文件就從版本庫(kù)中被刪除了。
另一種情況是刪錯(cuò)了,因?yàn)榘姹編?kù)里還有呢,所以可以很輕松地把誤刪的文件恢復(fù)到最新版本:
$ git checkout -- test.txt
git checkout其實(shí)是用版本庫(kù)里的版本替換工作區(qū)的版本,無論工作區(qū)是修改還是刪除,都可以“一鍵還原”。
相關(guān)文章
windows系統(tǒng)搭建WEB服務(wù)器詳細(xì)教程
這篇文章主要為大家詳細(xì)介紹了windows系統(tǒng)搭建WEB服務(wù)器詳細(xì)教程,文中安裝步驟介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08ssh服務(wù)器拒絕了密碼 請(qǐng)?jiān)僭囈淮我呀鉀Q(親測(cè)有效)
這篇文章主要介紹了解決ssh服務(wù)器拒絕了密碼 請(qǐng)?jiān)僭囈淮蔚膯栴},本文通過兩種方法給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08一文概括6種負(fù)載均衡技術(shù)的實(shí)現(xiàn)方式(小結(jié))
這篇文章主要介紹了一文概括6種負(fù)載均衡技術(shù)的實(shí)現(xiàn)方式(小結(jié)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05VScode連接遠(yuǎn)程服務(wù)器踩坑實(shí)戰(zhàn)記錄(新版離線vscode-server安裝)
本文主要介紹了如何使用VScode連接遠(yuǎn)程服務(wù)器,并對(duì)離線安裝vscode-server進(jìn)行了詳細(xì)的操作步驟說明,其中包括VScode擴(kuò)展的安裝與配置,vscode-server的離線下載,文件的解壓縮和移動(dòng),以及VScode的一些更新設(shè)置,能夠幫助讀者更好地理解和掌握VScode連接遠(yuǎn)程服務(wù)器的方法2024-10-10Dell R710 服務(wù)器做Raid0與Raid5磁盤陣列的圖文教程
這篇文章主要介紹了Dell R710 服務(wù)器做Raid0與Raid5磁盤陣列的圖文教程,需要的朋友可以參考下2018-05-05