为什么 git rm --cached 不删除本地跟踪过的文件,而是删除其他文件 [英] Why git rm --cached not remove local ever tracked file but others

查看:38
本文介绍了为什么 git rm --cached 不删除本地跟踪过的文件,而是删除其他文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在 git 存储库中取消跟踪文件时,使用 git rm -r --cached ..这不会删除本地存储中曾经跟踪过的文件,但是当其他开发人员使用 git pull 获取此提交时,他们机器存储中的曾经跟踪过的文件将被删除.

When untrack a file in the git repository, use git rm -r --cached .. This will not remove the ever tracked file in local storage, but when other developers fetch this commit with git pull, the ever tracked file will been removed on their machine storage.

您可以通过以下方式重现它:

You can reproduce it with:

  1. 保存当前工作.(机器 A)

git add .
git stash save "work position"

  1. 创建一个新文件并提交.(机器 A)

echo hello>>file_not_to_track
git add .
git commit -m "add file file_not_to_track"

  1. 从另一台机器(或其他目录)拉取(机器 B)

git pull

现在显示文件

ls
file_not_to_track  README.md

  1. 取消跟踪文件.(机器 A)

echo file_not_to_track >> .gitignore
git rm -r --cached .
git add .
git commit -m "untrack file_not_to_track"
git push

现在显示文件

ls
file_not_to_track  README.md

  1. 获取代码(机器 B)

git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From example.com:my/example_project
   6525df1..f413f8b  master     -> origin/master
Updating 6525df1..f413f8b
Fast-forward
 .gitignore        | 1 +
 file_not_to_track | 1 -
 2 files changed, 1 insertion(+), 1 deletion(-)
 create mode 100644 .gitignore
 delete mode 100644 file_not_to_track

现在显示文件

ls
README.md

正如它显示的那样 git rm -r --cached . 删除其他仓库中跟踪过的文件,但不在当前仓库中.

As it shows git rm -r --cached . remove ever tracked file on others repo but not in current repo.

推荐答案

On Machine A:

git rm -r --cached .

以上命令将从索引中删除文件(README.md 和 file_not_to_track).此时,索引为空.但是,file_not_to_track 仍然存在于文件系统中.

Above command will remove files from the index (both README.md and file_not_to_track). At this time, the index is empty. However, file_not_to_track is still exists on file system.

--cached:使用此选项仅从索引中取消暂存和删除路径.工作树文件,无论是否修改,都将保持不变.

--cached: Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone.

git add .

使用 add 操作,git 只添加了 README 文件.(file_not_to_track 已被忽略).

With add action, git just added only README file. (file_not_to_track has been ignored).

在机器 B 上:

2 个文件更改,1 个插入(+),1 个删除(-)

2 files changed, 1 insertion(+), 1 deletion(-)

通过 pull 操作,git 识别出 file_not_to_track 已经消失.Git 执行删除操作.

With pull action, git recognizes that file_not_to_track is gone. Git perform an detele action.

这篇关于为什么 git rm --cached 不删除本地跟踪过的文件,而是删除其他文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆