Git使用3:Git文件删除

Git文件删除

  • git rm filename

    • 删除工作区及暂存区中的该文件 相当于删除后执行git add
    • git rm --cached filename 在不小心将不需要追踪的文件添加到暂存区,想删除暂存的文件,但是不想删除工作区的文件很有用
    • git rm -f filename 当工作区或者暂存区文件修改了(作用:防止把修改误删除了)
    • glob模式
  • git mv oldname newname

    • 相当于:
    1. mv oldname newname
    2. git rm oldname
    3. git add newname

└─Demo
        index.html
        index-duplicate.html
        style.css

通常情况下删除文件的步骤:

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ ls -al
total 23
drwxr-xr-x 1 ONEFINE 197609   0 2月  10 13:33 ./
drwxr-xr-x 1 ONEFINE 197609   0 2月   9 19:54 ../
drwxr-xr-x 1 ONEFINE 197609   0 2月  10 13:36 .git/
-rw-r--r-- 1 ONEFINE 197609 158 2月  10 09:56 index.html
-rw-r--r-- 1 ONEFINE 197609 158 2月  10 09:56 index-duplicate.html
-rw-r--r-- 1 ONEFINE 197609   6 2月   8 10:12 style.css

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ rm -f index-duplicate.html

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ ls -al
total 22
drwxr-xr-x 1 ONEFINE 197609   0 2月  10 13:54 ./
drwxr-xr-x 1 ONEFINE 197609   0 2月   9 19:54 ../
drwxr-xr-x 1 ONEFINE 197609   0 2月  10 13:36 .git/
-rw-r--r-- 1 ONEFINE 197609 158 2月  10 09:56 index.html
-rw-r--r-- 1 ONEFINE 197609   6 2月   8 10:12 style.css

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ 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:    index-duplicate.html

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

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git add .

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    index-duplicate.html


ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$

接下来,我们新建一个文件并添加到工作区进行讲解:

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ touch tmp.html

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        tmp.html

nothing added to commit but untracked files present (use "git add" to track)

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git commit -am 'this newly!'
On branch master
Untracked files:
        tmp.html

nothing added to commit but untracked files present

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git add tmp.html

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git commit --amend
[master 78d9988] new!!!!
 Date: Sun Feb 10 14:20:45 2019 +0800
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename 1.txt => tmp.html (100%)

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git status
On branch master
nothing to commit, working tree clean

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$

在这基础上

git rm filename

git rm filename将工作区和暂存区文件一起删除。

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git rm tmp.html
rm 'tmp.html'

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    tmp.html


ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$

这个命令有什么用呢?我们修改style.css文件:

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ vim style.css

.{
        text-align:cent;
        color: blue;
}

:wq
ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    tmp.html

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:   style.css


ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git rm style.css
error: the following file has local modifications:
    style.css
(use --cached to keep the file, or -f to force removal)

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$

提示说删除失败:使用--cached删除暂存区文件而保持其在工作区中的状态,使用-f一起删除工作区和暂存区文件。

git rm --cached filename
ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git rm --cached style.css
rm 'style.css'

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    style.css
        deleted:    tmp.html

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

        style.css


ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ ls -al
total 22
drwxr-xr-x 1 ONEFINE 197609   0 2月  10 14:37 ./
drwxr-xr-x 1 ONEFINE 197609   0 2月   9 19:54 ../
drwxr-xr-x 1 ONEFINE 197609   0 2月  10 14:40 .git/
-rw-r--r-- 1 ONEFINE 197609 158 2月  10 09:56 index.html
-rw-r--r-- 1 ONEFINE 197609  37 2月  10 14:37 style.css

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$

使用--cached仅仅删除暂存区文件,工作区中的文件不受影响。
当我们修改了工作区或暂存区的文件的时候使用git rm命令会禁止我们删除,起到防止误删除作用。
git rm --cached filename 在不小心将不需要追踪的文件添加到暂存区,想删除暂存区的文件,但是不想删除工作区的文件很有用。

git rm -f filename

先将style.css文件从仓库拉回暂存区中,并从暂存区拉回工作区。

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git reset HEAD style.css
Unstaged changes after reset:
M       style.css

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git checkout style.css

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    tmp.html


ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$

我们执行 git rm -f filename

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git rm -f style.css
rm 'style.css'

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    style.css
        deleted:    tmp.html


ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ ls -al
total 21
drwxr-xr-x 1 ONEFINE 197609   0 2月  10 14:56 ./
drwxr-xr-x 1 ONEFINE 197609   0 2月   9 19:54 ../
drwxr-xr-x 1 ONEFINE 197609   0 2月  10 14:56 .git/
-rw-r--r-- 1 ONEFINE 197609 158 2月  10 09:56 index.html

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$

可以看到他把暂存区和工作区的文件都给删除了!

git mv oldname newname

将style.css返还工作区:

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    style.css
        deleted:    tmp.html


ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git reset HEAD style.css
Unstaged changes after reset:
D       style.css

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git checkout style.css

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    tmp.html


ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$

接下来:

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ mv index.html index1.html

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ ls -al
total 22
drwxr-xr-x 1 ONEFINE 197609   0 2月  10 15:18 ./
drwxr-xr-x 1 ONEFINE 197609   0 2月   9 19:54 ../
drwxr-xr-x 1 ONEFINE 197609   0 2月  10 15:15 .git/
-rw-r--r-- 1 ONEFINE 197609 158 2月  10 09:56 index1.html
-rw-r--r-- 1 ONEFINE 197609   9 2月  10 15:15 style.css

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    tmp.html

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:    index.html

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

        index1.html


ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$

将其添加到工作区:

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git add .

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        renamed:    index.html -> index1.html
        deleted:    tmp.html


ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$

提示被重命名了。


接下来使用git mv命令:

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git mv style.css style1.css

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        renamed:    index.html -> index1.html
        renamed:    style.css -> style1.css
        deleted:    tmp.html


ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$

posted @ 2019-02-10 10:38  onefine  阅读(486)  评论(0编辑  收藏  举报