为什么git rm --cached不会删除本地曾经跟踪的文件,而是其他 [英] Why git rm --cached not remove local ever tracked file but others
问题描述
取消跟踪git存储库中的文件时,请使用 git rm -r --cached。
。这不会删除本地存储中曾经跟踪的文件,但是当其他开发人员使用 git pull
获取此提交时,曾经跟踪的文件将在其计算机存储中被删除。
您可以使用以下方式重现它:
- 保存当前工作。 (机器A )
git add。
git stash保存工作位置
- 创建一个新文件并提交。(机器A )
echo hello>> file_not_to_track
git add。
git commit -m添加文件file_not_to_track
- 从另一台计算机(或其他目录)中拉出(机器B )
git pull
立即显示文件
ls
file_not_to_track README.md
< ol start = 4>
echo file_not_to_track>> .gitignore
git rm -r-缓存。
git add。
git commit -m取消跟踪file_not_to_track
git push
立即显示文件
ls
file_not_to_track README.md
- 获取代码(机器B )
git pull
远程:计数对象:3,完成。
remote:压缩对象:100%(2/2),已完成。
远程:总计3个(增量0),重用0(增量0)
拆包对象:100%(3/3),已完成。
from example.com:my/example_project
6525df1..f413f8b主菜单-> origin / master
正在更新6525df1..f413f8b
快进
.gitignore | 1 +
file_not_to_track | 1-
2个文件已更改,1个插入(+),1个删除(-)
创建模式100644 .gitignore
删除模式100644 file_not_to_track
立即显示文件
ls
自述文件.md
它显示 git rm -r --cached。
删除其他存储库上已跟踪的文件,但不删除当前存储库中的文件。
在计算机A上:
git rm -r --cached。
以上命令将从索引中删除文件( README.md和file_not_to_track )。目前,索引为空。但是,file_not_to_track在文件系统上仍然存在。
-已缓存:使用此选项取消登台并仅从索引中删除路径。正在工作的树文件,无论是否已修改,都将被保留。
git add。
通过 add 操作,git只添加了 README 文件。 (file_not_to_track已被忽略)。
在计算机B上:
更改了2个文件,插入了1个(+),删除了1个(-)
使用拉动操作,git意识到 file_not_to_track 不见了。 Git执行了一个动作。
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:
- save current work. (Machine A)
git add .
git stash save "work position"
- create a new file and commit it.(Machine A)
echo hello>>file_not_to_track
git add .
git commit -m "add file file_not_to_track"
- pull from another machine(or another directory)(Machine B)
git pull
show files now
ls
file_not_to_track README.md
- untrack the file.(Machine A)
echo file_not_to_track >> .gitignore
git rm -r --cached .
git add .
git commit -m "untrack file_not_to_track"
git push
show files now
ls
file_not_to_track README.md
- fetch code(Machine 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
show files now
ls
README.md
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 .
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: 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 .
With add action, git just added only README file. (file_not_to_track has been ignored).
On Machine B:
2 files changed, 1 insertion(+), 1 deletion(-)
With pull action, git recognizes that file_not_to_track is gone. Git perform an detele action.
这篇关于为什么git rm --cached不会删除本地曾经跟踪的文件,而是其他的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!