- 10.2 删除文件
- On branch master
- Changes to be committed:
- (use "git reset HEAD<file>…"to unstage)
- new file:hack-1.txt
- Changed but not updated:
- (use "git add<file>…"to update what will be committed)
- (use "git checkout—<file>…"to discard changes in working directory)
- modified:welcome.txt
- On branch master
- Changes to be committed:
- (use "git reset HEAD<file>…"to unstage)
- new file:hack-1.txt
- Changed but not updated:
- (use "git add/rm<file>…"to update what will be committed)
- (use "git checkout—<file>…"to discard changes in working directory)
- deleted:detached-commit.txt
- deleted:hack-1.txt
- deleted:new-commit.txt
- deleted:welcome.txt
10.2 删除文件
看看版本库当前的状态,暂存区和工作区都包含修改。
$git status-s
A hack-1.txt
M welcome.txt
在这个暂存区和工作区都包含文件修改的情况下,使用删除命令更具有挑战性。删除命令有多种使用方法,有的方法很巧妙,而有的方法则需要更多的输入。为了分别介绍不同的删除方法,还要使用上一章介绍的进度保存(git stash)命令。
保存进度。
$git stash
Saved working directory and index state WIP on master:2b31c19 Merge commit
'acc2f69'
HEAD is now at 2b31c19 Merge commit 'acc2f69'
再恢复进度。注意不要使用git stash pop,而是使用git stash apply,因为这个保存的进度要被多次用到。
$git stash apply
On branch master
Changes to be committed:
(use "git reset HEAD<file>…"to unstage)
#
new file:hack-1.txt
#
Changed but not updated:
(use "git add<file>…"to update what will be committed)
(use "git checkout—<file>…"to discard changes in working directory)
#
modified:welcome.txt
#
10.2.1 本地删除不是真的删除
当前工作区的文件有:
$ls
detached-commit.txt
hack-1.txt
new-commit.txt
welcome.txt
直接在工作区删除这些文件,会如何呢?
$rm*.txt
通过下面的命令,可以看到在暂存区(版本库)中的文件仍然存在,并未删除。
$git ls-files
detached-commit.txt
hack-1.txt
new-commit.txt
welcome.txt
从文件的状态来看,文件只是在本地进行了删除,尚未添加到暂存区(提交任务)中。也就是说:直接在工作区删除,对暂存区和版本库没有任何影响。
$git status
On branch master
Changes to be committed:
(use "git reset HEAD<file>…"to unstage)
#
new file:hack-1.txt
#
Changed but not updated:
(use "git add/rm<file>…"to update what will be committed)
(use "git checkout—<file>…"to discard changes in working directory)
#
deleted:detached-commit.txt
deleted:hack-1.txt
deleted:new-commit.txt
deleted:welcome.txt
#
从Git状态输出中可以看出,本地删除如果要反映在暂存区中应该用git rm命令,对不想删除的文件执行git checkout—<file>,可以让文件在工作区重现。