Git常用命令rm

这篇具有很好参考价值的文章主要介绍了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 root 197121 10 520 09:42 a.txt
-rw-r--r-- 1 root 197121  0 520 09:58 b.txt
-rw-r--r-- 1 root 197121  0 520 09:41 c.txt
-rw-r--r-- 1 root 197121  0 520 09:41 d.txt
-rw-r--r-- 1 root 197121  0 520 09:42 e.txt
-rw-r--r-- 1 root 197121  0 520 09:41 f.txt
-rw-r--r-- 1 root 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 root 197121 10 520 09:42 a.txt
-rw-r--r-- 1 root 197121  0 520 09:58 b.txt
-rw-r--r-- 1 root 197121  0 520 09:41 c.txt
-rw-r--r-- 1 root 197121  0 520 09:41 d.txt
-rw-r--r-- 1 root 197121  0 520 09:41 f.txt
-rw-r--r-- 1 root 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 root 197121 10 520 09:42 a.txt
-rw-r--r-- 1 root 197121  0 520 09:58 b.txt
-rw-r--r-- 1 root 197121  0 520 09:41 c.txt
-rw-r--r-- 1 root 197121  0 520 09:41 d.txt
-rw-r--r-- 1 root 197121  0 520 09:42 e.txt
-rw-r--r-- 1 root 197121  0 520 09:41 f.txt
-rw-r--r-- 1 root 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 root 197121 10 520 09:42 a.txt
-rw-r--r-- 1 root 197121  0 520 09:41 b.txt
-rw-r--r-- 1 root 197121  0 520 09:41 c.txt
-rw-r--r-- 1 root 197121  0 520 09:41 d.txt
-rw-r--r-- 1 root 197121  0 520 09:41 f.txt
-rw-r--r-- 1 root 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 root 197121 10 520 09:42 a.txt
-rw-r--r-- 1 root 197121  0 520 09:41 b.txt
-rw-r--r-- 1 root 197121  0 520 09:41 c.txt
-rw-r--r-- 1 root 197121  0 520 09:41 d.txt
-rw-r--r-- 1 root 197121  0 520 09:42 e.txt
-rw-r--r-- 1 root 197121  0 520 09:41 f.txt
-rw-r--r-- 1 root 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 root 197121 10 520 09:42 a.txt
-rw-r--r-- 1 root 197121  0 520 09:41 b.txt
-rw-r--r-- 1 root 197121  0 520 09:41 c.txt
-rw-r--r-- 1 root 197121  0 520 09:41 d.txt
-rw-r--r-- 1 root 197121  0 520 09:42 e.txt
-rw-r--r-- 1 root 197121  0 520 09:41 f.txt
-rw-r--r-- 1 root 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 root 197121 10 520 09:42 a.txt
-rw-r--r-- 1 root 197121  0 520 09:41 b.txt
-rw-r--r-- 1 root 197121  0 520 09:41 c.txt
-rw-r--r-- 1 root 197121  0 520 09:41 d.txt
-rw-r--r-- 1 root 197121  0 520 09:42 e.txt
-rw-r--r-- 1 root 197121  0 520 09:41 f.txt
-rw-r--r-- 1 root 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、场景四

使用场景:你不小心提交了不该提交的文件,想把它从提交中删除,本地也不保留该文件:文章来源地址https://www.toymoban.com/news/detail-755356.html

$ 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

到了这里,关于Git常用命令rm的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • Git下载和Git常用命令

    要下载和安装 Git,请按照以下步骤进行操作: 1.访问官方网站:打开 Git 官方网站 https://git-scm.com/。 2.下载 Git 安装程序: 3.选择适用于你的操作系统的下载链接。Git 支持多个操作系统,包括 Windows、macOS 和 Linux。 4.点击下载链接,开始下载 Git 安装程序。 5.运行安装程序:

    2024年02月08日
    浏览(10)
  • 【Git】git常用命令集合

      Git是现代软件开发不可或缺的版本控制工具,它可以追踪文件的变化、协同开发以及管理代码版本。掌握Git的常用命令对于开发人员来说至关重要。本文将为你介绍一些Git常用命令,帮助你轻松驾驭版本控制。   详细介绍了Git的常用命令,涵盖了版本管理、分支操作、

    2023年04月08日
    浏览(11)
  • git快速入门!!! git的常用命令!!!

    Git 是一个开源的分布式版本控制系统,有很多实用的命令可以帮助我们更高效地管理项目和代码。以下是一些常用的 Git 命令及其使用方法和示例: 1. 初始化一个新的 Git 仓库 这个命令会在当前目录下创建一个新的 Git 仓库。 2. 添加文件到暂存区 这个命令会把指定的文件添

    2024年02月05日
    浏览(12)
  • git 常用命令之 git checkout

    git 常用命令之 git checkout

    大家好,我是 17。 git checkout 是 git 中最重要最常用的命令之一,本文为大家详细解说一下。 checkout 的用途之一是恢复工作区。 checkout . 表示恢复工作区的所有更改,未跟踪的文件不会有变化。 恢复工作区的所有文件风险比较大,会丢失所有工作区的修改,一定要慎用 中间加

    2023年04月13日
    浏览(10)
  • Git系列讲解(五):Git常用命令整理

    Git系列讲解(五):Git常用命令整理

    Git系列的前几篇文章针对基础知识进行了详细讲解,但是Git还包含很多其他命令,就不每个都展开细讲了,本篇文章整理了一些2.0+版本的常用Git命令,以供备忘。 1.1 git clone url 本地路径 克隆远程版本库到本地所指定的路径中,包括代码,分支和版本的提交记录等; 若后面不

    2024年02月04日
    浏览(13)
  • 【Git】git环境如何搭建与其常用命令

    搭建 Git 环境: 安装 Git 客户端:根据操作系统选择对应的版本进行下载安装,下载地址:https://git-scm.com/downloads。 配置 Git 用户名和邮箱:打开 Git Bash(Windows 系统)或终端(Mac、Linux 系统),输入以下命令进行配置。 常用 Git 命令: 初始化 Git 仓库:在项目目录下通过以下

    2023年04月25日
    浏览(12)
  • git常用命令(git github ssh)

    git常用命令(git github ssh)

    被” “和\\\"[ ]“包含起来的内容表示用户自己选定的参数。但” “是要求用户必须输入的,而”[ ]\\\"表示用户可以根据自己的需要选择输入。 比如git reset的语法是这样的: 其中commitid指的是commit id,可以理解为每一提交到本地仓库之后该仓库状态的ID,利用这个ID我们可以快速

    2024年02月03日
    浏览(11)
  • 【git】工作场景中常用的git命令

    工作场景中常用的git命令,记录下来方便调取 一般与他人合作,至少你提交的名字得被人熟知或者遵循规范,因此需要更改名字和邮箱 全局修改 只对本项目的修改 有时候你想使用简略的命令如直接 git push 而不指定远程分支,则需要设置一个远程分支作为你的上游分支 我们

    2024年02月11日
    浏览(27)
  • Git学习笔记(第3章):Git常用命令

    Git学习笔记(第3章):Git常用命令

    目录 3.1 设置用户签名 3.2 初始化本地库 3.3 查看本地库状态 3.4 添加暂存区 3.5 提交本地库 3.6 历史版本 3.7 修改文件 3.8 版本穿梭 小结 命令 作用 git config --global user.name 用户名 设置用户签名 git config --global user.email 邮箱 设置用户签名 git init 初始化本地库 git status 查看本地库状

    2024年01月20日
    浏览(19)
  • Git常用命令(Git Bash Here版)

    Git常用命令(Git Bash Here版)

    目录 一、Git常用命令 1、设置用户签名 2、初始化本地库 3、查看本地库状态 4、新增文件 5、添加到暂存区 6、提交本地库  7、修改文件 7、查看历史版本 8、版本穿梭 9、创建分支 10、查看分支 11、修改分支 12、切换分支 13、合并分支 二、GitHub 操作 1、创建远程仓库 2、创建

    2024年02月06日
    浏览(9)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包