Git之刪除檔案學習教程

2021-07-16 06:45:52 字數 1280 閱讀 9936

在git中,刪除也是乙個修改操作,

這裡我們

用例項來

學習

git

,先新增乙個新檔案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

這個時候,git知道你刪除了檔案,因此,工作區和版本庫就不一致了,git status命令會立刻告訴你哪些檔案被刪除了:

$ git status# on branch master# changes not staged for commit:#   (use "git add/rm ..." to update what will be committed)#   (use "git checkout -- ..." to discard changes in working directory)##       deleted:    test.txt#

no changes added to commit (use "git add" and/or "git commit -a")

現在你有兩個選擇,一是確實要從版本庫中刪除該檔案,那就用命令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

現在,檔案就從版本庫中被刪除了。

另一種情況是刪錯了,因為版本庫里還有呢,所以可以很輕鬆地把誤刪的檔案恢復到最新版本:

$ git checkout -- test.txt

git checkout其實是用版本庫里的版本替換工作區的版本,無論工作區是修改還是刪除,都可以「一鍵還原」。

小結

命令git rm用於刪除乙個檔案。如果乙個檔案已經被提交到版本庫,那麼你永遠不用擔心誤刪,但是要小心,你只能恢復檔案到最新版本,你會丟失最近一次提交後你修改的內容。

Git教程 刪除檔案

在git中,刪除也是乙個修改操作,我們實戰一下,先新增乙個新檔案test.txt到git並且提交 git add test.txt git commit m add test.txt master 94cdc44 add test.txt 1 file changed,1 insertion cre...

git學習 刪除檔案

在git中,刪除也是乙個修改操作,我們實戰一下,先新增乙個新檔案test.txt到git並且提交 git add test.txt git commit m add test.txt master b84166e add test.txt 1 file changed,1 insertion cre...

git完全教程 007 Git刪除檔案

在刪除檔案之前得有這個檔案,所以我們先新增,順便複習前面的內容 在倉庫中新增乙個新的檔案test.txt並提交 通常刪除檔案,直接rm即可 此時,git知道了你刪除檔案,工作區和版本庫不一致了,git status會告訴你哪些檔案被刪除 此時你就有兩個選擇 第一種 確實要從版本庫中刪除該檔案,使用g...