git 入門教程之刪除文件

刪除文件

回憶一下文件的常見操做,新增文件,修改文件,刪除文件等,新增和修改文件都單獨討論過,如今咱們來研究一下如何刪除文件.git

你可能會說刪除文件還不簡單啊,直接 rm -rf <file> 便可,可是這僅僅是本地文件被刪除了,對於 git 來講,文件並無被刪除.bash

還記得咱們開篇介紹git 時就說過,一切操做皆版本 ,對於新增是一個版本,修改也是一個版本,就連刪除都是一個版本.spa

下面讓咱們看一下 git 中如何刪除文件吧!code

背景

# 查看當前文件列表
$ ls
file1.txt   file2.txt   file3.txt   newFile.txt test.txt
# 新建待刪除文件
$ touch delete.txt
# 再次查看當前文件列表,確保新建文件成功
$ ls
delete.txt  file2.txt   newFile.txt
file1.txt   file3.txt   test.txt
# 查看當前文件狀態: 新文件 `delete.txt` 還沒被跟蹤
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .DS_Store
    delete.txt

nothing added to commit but untracked files present (use "git add" to track)
# 添加新文件 `delete.txt`
$ git add delete.txt
# 查看文件狀態: 已添加到暫存區,待提交到版本庫
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   delete.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .DS_Store

# 提交新文件 `delete.txt`
$ git commit -m "add delete.txt"
[master 7df386a] add delete.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 delete.txt
# 再次查看文件狀態: 已經沒有新文件 `delete.txt` 的更改信息
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .DS_Store

nothing added to commit but untracked files present (use "git add" to track)
$ 
複製代碼

以上操做,咱們簡單建立 delete.txt 文件,添加(git add)並提交(git commit) 該文件,完成準備工做後,開始刪除文件!rem

# 刪除前文件列表
$ ls
delete.txt  file2.txt   newFile.txt
file1.txt   file3.txt   test.txt
# 刪除剛剛建立的文件 `delete.txt`
$ rm delete.txt
# 刪除後文件列表
$ ls
file1.txt   file2.txt   file3.txt   newFile.txt test.txt
# 當前文件狀態: `delete.txt` 文件已被刪除,且未添加到暫存區
$ 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:    delete.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .DS_Store

no changes added to commit (use "git add" and/or "git commit -a")
$ 
複製代碼

本地刪除 delete.txt 文件後,再次查看文件狀態 git status 發現 git 給了咱們兩條建議,其中一條 git checkout -- <file> 咱們很熟悉,就是丟棄工做區的更改,此時此景下若是丟棄刪除操做,至關於撤銷刪除,難怪說刪除也是一個版本呢!string

如今咱們重點來看第一條建議 git add/rm <file> ,rmremove 單詞的縮寫,即刪除文件.it

# 刪除文件
$ git rm delete.txt
rm 'delete.txt'
# 查看文件狀態: `delete.txt` 文件待提交
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    deleted:    delete.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .DS_Store

# 提交文件
$ git commit -m "remove delete.txt"
[master 6298070] remove delete.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 delete.txt
# 再次查看文件狀態
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .DS_Store

nothing added to commit but untracked files present (use "git add" to track)
$ 
複製代碼

刪除文件和添加文件相似,都是一次commit ,本地文件的任何更改都要添加到暫存區,而後提交到版本庫.io

小結

刪除文件和新增文件相似邏輯,git rm 刪除文件後,依然須要 git commit 提交版本.ast

相關文章
相關標籤/搜索