您现在的位置是:首页 >技术教程 >Git常用命令rm网站首页技术教程

Git常用命令rm

zsx_yiyiyi 2024-06-17 11:27:51
简介Git常用命令rm

Git常用命令rm

删除暂存区文件:

# 从暂存区和工作区中删除文件
$ git rm [file1] [file2] ...
# 把文件从暂存区域移除,但该文件会保留在工作区
$ git rm --cached file
# 如果删除之前修改过并且已经放到暂存区域
# 强行从暂存区和工作区中删除修改后的文件
$ git rm -f file
# 递归删除
# 在删除文件夹的时候,使用参数-r表示循环删除文件夹中的内容
$ git rm -r *

Git 本地数据管理,大概可以分为三个区:

  • 工作区:是可以直接编辑的地方。

  • 暂存区:数据暂时存放的区域。

  • 版本库:存放已经提交的数据。

工作区的文件 git add 后到暂存区,暂存区的文件 git commit 后到版本库。

1、linux rm删除

rm 命令的使用效果就是删除工作区中的文件。

因此想要把删除提交到本地仓库,还需要执行 git addgit commit两个命令。

$ ll
total 2
-rw-r--r-- 1 zhangshixing 197121 10 520 09:42 a.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:58 b.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:41 c.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:41 d.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:42 e.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:41 f.txt
-rw-r--r-- 1 zhangshixing 197121 14 520 09:42 new.txt

$ rm -rf e.txt

$ git status
On branch branch_a
Your branch is up-to-date with 'origin/branch_a'.

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:    e.txt

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

$ ll
total 2
-rw-r--r-- 1 zhangshixing 197121 10 520 09:42 a.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:58 b.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:41 c.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:41 d.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:41 f.txt
-rw-r--r-- 1 zhangshixing 197121 14 520 09:42 new.txt

$ git add .

$ git status
On branch branch_a
Your branch is up-to-date with 'origin/branch_a'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    e.txt

$ git commit -m "delete e.txt"
[branch_a 8a3735c] delete e.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 e.txt

$ git status
On branch branch_a
Your branch is ahead of 'origin/branch_a' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

删除了工作区和版本库的文件。

2、git rm删除

git rm 会删除工作区文件,并且将这次删除放入暂存区。

git rm 相当于 linux rm + git add 命令。

要删除的文件是没有修改过的,就是说和当前版本库文件的内容相同。

git commit 后,版本库中的此文件记录也会被删除。

$ ll
total 2
-rw-r--r-- 1 zhangshixing 197121 10 520 09:42 a.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:58 b.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:41 c.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:41 d.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:42 e.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:41 f.txt
-rw-r--r-- 1 zhangshixing 197121 14 520 09:42 new.txt

$ git rm e.txt
rm 'e.txt'

# 该命令执行的结果已经git add过了
$ git status
On branch branch_a
Your branch is up-to-date with 'origin/branch_a'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    e.txt

$ ll
total 2
-rw-r--r-- 1 zhangshixing 197121 10 520 09:42 a.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:41 b.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:41 c.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:41 d.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:41 f.txt
-rw-r--r-- 1 zhangshixing 197121 14 520 09:42 new.txt

$ git commit -m "delete e.txt"
[branch_a a290057] delete e.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 e.txt

$ git status
On branch branch_a
Your branch is ahead of 'origin/branch_a' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

删除了工作区和版本库的文件,因为暂存区不可能有该文件。

如果暂存区有该文件意味着该文件修改后 git add 到了暂存区,那样 git rm 命令会报错。

或者是如果修改了工作区的文件,那么 git rm 命令也会报错。

3、git rm -f删除

文件修改后不管有没有 git add 到暂存区,使用 git rm 命令删除都会报错。

3.1 情况一

当工作区中的文件经过修改后,再想使用 git rm 命令时,就需要添加 -f 参数,表示强制删除工作区中的文件,并

将删除添加到暂存区;

$ ll
total 2
-rw-r--r-- 1 zhangshixing 197121 10 520 09:42 a.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:41 b.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:41 c.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:41 d.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:42 e.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:41 f.txt
-rw-r--r-- 1 zhangshixing 197121 14 520 09:42 new.txt

$ echo b > b.txt

$ cat b.txt
b

$ git status
On branch branch_a
Your branch is up-to-date with 'origin/branch_a'.

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:   b.txt

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

$ git rm b.txt
error: the following file has local modifications:
    b.txt
(use --cached to keep the file, or -f to force removal)
# 解决
# 使用 git rm -f 命令进行删除的效果
$ git rm -f b.txt
rm 'b.txt'

$ git status
On branch branch_a
Your branch is up-to-date with 'origin/branch_a'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    b.txt

# 提交到版本库
$ git commit -m "delete b.txt"
[branch_a 73a416b] delete b.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 b.txt

$ git status
On branch branch_a
Your branch is ahead of 'origin/branch_a' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

3.2 情况二

当工作区中的文件经过修改后,使用 git add 命令添加到暂存区后,再想使用 git rm 命令时,就需要添加 -f 参

数,表示强制删除工作区中和暂存区中的文件,并将删除添加到暂存区;

$ ll
total 2
-rw-r--r-- 1 zhangshixing 197121 10 520 09:42 a.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:41 b.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:41 c.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:41 d.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:42 e.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:41 f.txt
-rw-r--r-- 1 zhangshixing 197121 14 520 09:42 new.txt

$ echo b > b.txt

$ cat b.txt
b

$ git status
On branch branch_a
Your branch is up-to-date with 'origin/branch_a'.

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:   b.txt

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

$ git add b.txt
warning: LF will be replaced by CRLF in b.txt.
The file will have its original line endings in your working directory.

$ git rm b.txt
error: the following file has changes staged in the index:
    b.txt
(use --cached to keep the file, or -f to force removal)
# 解决
$ git rm -f b.txt
rm 'b.txt'

$ git status
On branch branch_a
Your branch is up-to-date with 'origin/branch_a'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    b.txt

$ git commit -m "delete b.txt"
[branch_a d076cf3] delete b.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 b.txt

$ git status
On branch branch_a
Your branch is ahead of 'origin/branch_a' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

上面的两种情况删除了工作区、暂存区和版本库的文件。

4、git rm --cached删除

git rm --cached 会删除暂存区中的文件,但是会保留工作区中的文件,并将此次删除提交到暂存区。

文件从暂存区中删除掉,即不会被提交到版本库中,也就是说此文件被取消了版本控制。

–cached 参数删除的文件必须是已经被追踪的文件,即之前被版本控制的文件。

使用场景:你不小心提交了不该提交的文件,想把它从提交中删除,但是又想在本地保留该文件。

$ ll
total 2
-rw-r--r-- 1 zhangshixing 197121 10 520 09:42 a.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:41 b.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:41 c.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:41 d.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:42 e.txt
-rw-r--r-- 1 zhangshixing 197121  0 520 09:41 f.txt
-rw-r--r-- 1 zhangshixing 197121 14 520 09:42 new.txt

$ git rm --cache b.txt
rm 'b.txt'

$ ls
a.txt  b.txt  c.txt  d.txt  e.txt  f.txt  new.txt

$ git status
On branch branch_a
Your branch is up-to-date with 'origin/branch_a'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    b.txt

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

        b.txt

$ git commit -m "delete b.txt"
[branch_a c5fcd52] delete b.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 b.txt

# 说明本地多了b.txt
$ git status
On branch branch_a
Your branch is ahead of 'origin/branch_a' by 1 commit.
  (use "git push" to publish your local commits)

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

        b.txt

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

该命令删除了暂存区和版本库的文件,但保留了工作区的文件。

如果文件有修改并 git add 到暂存区,再执行 git rm --cached 和 git commit,那么保留的工作区文件是修改后的

文件,同时暂存区的修改文件和版本库的文件也被删了。

5、场景一

我们新添加了一个文件,执行了 git add 添加到了暂存区,发现该文件没有用,想要删除该文件:

$ touch temp.txt

$ echo  temp > temp.txt

$ git add temp.txt
warning: LF will be replaced by CRLF in temp.txt.
The file will have its original line endings in your working directory.

$ git status
On branch branch_a
Your branch is up-to-date with 'origin/branch_a'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   temp.txt

# 错误执行
# git rm会报错
$ git rm temp.txt
error: the following file has changes staged in the index:
    temp.txt
(use --cached to keep the file, or -f to force removal)

# 正确执行
# 删除工作区和暂存区文件,并且将这次删除放入暂存区
$ git rm -f temp.txt
rm 'temp.txt'

$ ls
# 没有文件

# 将这次删除放入暂存区是指恢复到new file之前的状态
$ git status
On branch branch_a
Your branch is up-to-date with 'origin/branch_a'.

nothing to commit, working tree clean

6、场景二

我们新添加了一个文件,执行了 git add 添加到了暂存区,发现该文件有错误需要修改:

$ touch temp.txt

$ echo  temp > temp.txt

$ git add temp.txt
warning: LF will be replaced by CRLF in temp.txt.
The file will have its original line endings in your working directory.

$ git status
On branch branch_a
Your branch is up-to-date with 'origin/branch_a'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   temp.txt

# 错误执行
# git rm会报错
$ git rm temp.txt
error: the following file has changes staged in the index:
    temp.txt
(use --cached to keep the file, or -f to force removal)

# 正确执行
# 删除暂存区文件,但保留工作区的文件,并且将这次删除放入暂存区
$ git rm --cached temp.txt
rm 'temp.txt'

# # 将这次删除放入暂存区是指恢复到git add之前的状态
$ git status
On branch branch_a
Your branch is up-to-date with 'origin/branch_a'.

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

        temp.txt

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

$ ls
temp.txt

$ cat temp.txt
temp

7、场景三

使用场景:你不小心提交了不该提交的文件,想把它从提交中删除,但是又想在本地保留该文件。

$ touch myfile.txt

$ echo myfile > myfile.txt

$ git add myfile.txt

$ git commit -m "add myfile.txt"
[branch_a 59b8aac] add myfile.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 myfile.txt
# 第一种方式
$ git rm --cache myfile.txt
rm 'myfile.txt'

$ git status
On branch branch_a
Your branch is ahead of 'origin/branch_a' by 1 commit.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    myfile.txt

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

        myfile.txt

$ ls
myfile.txt

$ git commit -m "delete myfile.txt"
[branch_a 91477d4] delete myfile.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 myfile.txt

$ git status
On branch branch_a
Your branch is ahead of 'origin/branch_a' by 2 commits.
  (use "git push" to publish your local commits)

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

        myfile.txt

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

$ cat myfile.txt
echo myfile
# 第二种方式
# 也可以使用git rm和git reset命令
$ git rm myfile.txt
rm 'myfile.txt'

# git add之后的状态
$ git status
On branch branch_a
Your branch is ahead of 'origin/branch_a' by 1 commit.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    myfile.txt

        
$ ls
# 没有myfile.txt文件

$ git reset HEAD myfile.txt
Unstaged changes after reset:
D       myfile.txt

$ git status
On branch branch_a
Your branch is ahead of 'origin/branch_a' by 1 commit.
  (use "git push" to publish your local commits)

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:    myfile.txt

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

$ ls
# 没有文件

$ git checkout -- myfile.txt

$ ls
myfile.txt

$ cat myfile.txt
echo myfile

$ git status
On branch branch_a
Your branch is ahead of 'origin/branch_a' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

如果在配置 .gitignore 文件之前就把某个文件上传到远程仓库了,这时候想把远程仓库中的该文件删除,此时你配

置 .gitignore 文件也没有用,因为该文件已经被追踪了,但又不想在本地删除该文件后再重新提交到远程仓库,这

时候可以使用 git rm --cached filename 命令取消该文件的追踪,这样下次提交的时候,git 就不会再提交这

个文件,从而远程仓库的该文件也会被删除。

8、场景四

使用场景:你不小心提交了不该提交的文件,想把它从提交中删除,本地也不保留该文件:

$ touch myfile.txt

$ echo myfile > myfile.txt

$ git add myfile.txt

$ git commit -m "add myfile.txt"
[branch_a 59b8aac] add myfile.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 myfile.txt
$ git rm myfile.txt
rm 'myfile.txt'

$ git status
On branch branch_a
Your branch is ahead of 'origin/branch_a' by 1 commit.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    myfile.txt

$ ls
# 没有文件

$ git commit -m "delete myfile.txt"
[branch_a c074ead] delete myfile.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 myfile.txt

$ git status
On branch branch_a
Your branch is ahead of 'origin/branch_a' by 2 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。