分享

GIT基本快照命令之git-rm

 印度阿三17 2020-01-22

名称NAME

git-rm - 从工作树和索引中删除文件

概要SYNOPSIS

git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch] [--quiet] [--] <file>…

描述DESCRIPTION

从索引或工作树和索引中删除文件。

git rm不会仅从您的工作目录中删除文件。(没有选项options可以仅从工作树中删除文件并将其保留在索引中;如果要这样做,请使用/bin/rm)

删除的文件必须与分支的尖端相同,并且无内容的更新可以暂存到索引中,尽管可以使用-f选项覆盖默认行为。 如果指定了--cached,则暂存的内容必须与分支的尖端或磁盘上的文件匹配,从而允许仅从索引中删除文件。

选项OPTIONS

<file>…

要删除的文件

可以使用通配符(例如* .c)删除所有匹配的文件。可以指定一个前导目录名称(例如dir删除dir/file1和dir/file2)来删除目录中的所有文件,以及递归地删除所有子目录,但这要求显式给出-r选项。

-f    --force

Override the up-to-date check.

-n    --dry-run

实际上不删除任何文件。 而是显示它们是否存在于索引中,否则将被命令删除。.

-r

给定前导目录名称时,允许递归删除。

--

此选项可用于将命令行选项与文件列表分开(当文件名可能误认为命令行选项时很有用)。

--cached

使用此选项可以取消暂存并仅从索引中删除路径。 工作树文件,无论是否已修改,都将被保留。

--ignore-unmatch

即使没有文件匹配,也以零状态退出。

-q

--quiet

git rm 通常为删除的每个文件输出一行(以rm命令的形式)。 此选项禁止输出。

讨论DISCUSSION

给该命令的<file>列表可以是确切的路径名,通配符模式或前导目录名。 该命令仅删除Git已知的路径。 提供您未告知Git的文件名不会删除该文件。

文件遍历跨目录边界匹配。 因此,给定两个目录d和d2,使用git rm 'd *' 和git rm 'd/*' 是有区别的,因为前者还将删除所有目录d2。

删除已从文件系统中消失的文件

There is no option for git rm to remove from the index only the paths that have disappeared from the filesystem. However, depending on the use case, there are several ways that can be done.

没有选项option可以使 git rm 从索引中删除文件系统中已消失的路径。 但是,根据使用情况,有几种方法可以完成。

使用 “git commit -a”

If you intend that your next commit should record all modifications of tracked files in the working tree and record all removals of files that have been removed from the working tree with rm (as opposed to git rm), use git commit -a, as it will automatically notice and record all removals. You can also have a similar effect without committing by using git add -u.

使用 “git add -A”

When accepting a new code drop for a vendor branch, you probably want to record both the removal of paths and additions of new paths as well as modifications of existing paths.

Typically you would first remove all tracked files from the working tree using this command:

git ls-files -z | xargs -0 rm -f

and then untar the new code in the working tree. Alternately you could rsync the changes into the working tree.

After that, the easiest way to record all removals, additions, and modifications in the working tree is:

git add -A

Other ways

If all you really want to do is to remove from the index the files that are no longer present in the working tree (perhaps because your working tree is dirty so that you cannot use git commit -a), use the following command:

git diff --name-only --diff-filter=D -z | xargs -0 git rm --cached

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多