上一节《Git学习手记8:工作区、版本库和暂存区》
理解了工作区、暂存区和版本库后,理解删除文件就很容易了。
在工作区新建一个文件readme.txt,用git status,可以检测到readme.txt新建,但还没有添加到暂存区和版本库。
用命令
$ git add readme.txt
$ git commit -m "this is the 1st readme.txt for LearnGit.py"
并用git status 查看,可以知道文件已经commit到版本库了,working tree clean.
第一种,删除工作区文件,用命令
$ rm -v readme.txt
$ git status
可以检测到,工作区的文件被删除了,但是版本库里面的还留着。若想恢复,可以用命令:
$ git checkout readme.txt
可以看到,被删除了的readme.txt文件又恢复到工作区了。
$ git checkout [branch-name] 命令的意思就是 把指定的分支(branch)从版本库里面更新到工作区
Switches to the specified branch and updates the working directory
第二种,要同时删除工作区和暂存区的文件,用命令
$ git rm [file]
Deletes the file from the working directory and stages the deletion
然后用命令
$ git commit -m "[descriptive message]" 更新版本库
第三种,只删除版本库里面的文件,而保留工作区里面的文件,用命令:
$ git rm --cached [file]
Removes the file from version control but preserves the file locally
第四种,重命名文件,然后提交到版本库里面,用命令:
$ git mv [file-original] [file-renamed]
Changes the file name and prepares it for commit,如下图所示: