我克隆了包含三个文件的远程存储库
file1.txt
file2.txt
file3.txt
在我的机器上对 file3.txt 进行本地更改。我不希望更改进入远程仓库,反之亦然。
我读到使用 .gitignore 无法忽略已经提交和跟踪的文件。
我决定在 .git/info/exclude 文件中使用以下行
排除文件
file3.txt
但在 file3.txt 中所做的更改仍然显示为未暂存文件。我希望这个 file3.txt 在提交和暂存中被忽略,还需要防止 file3.txt 从远程仓库更新
在此先感谢您的帮助
I read that using .gitignore it is not possible to ignore the already commited and tracked file.
是的,使用 .gitignore
或 .git/info/exclude
。
您只需要先记录该文件的删除即可。
git rm --cached file3.txt
一旦它被删除(仅从缓存中,而不是从硬盘驱动器中),git status
不应显示它(因为它已经在 .gitignore
)
参见““git rm --cached x
” vs “git reset head — x
”?”:“缓存”,也称为“索引”或“暂存区”,仅从索引中删除文件并将其保存在您的工作副本中。
file3.txt
将不再在 repo 中进行版本控制。
一旦该删除被提交并推送,这将传播到其他克隆。
如果你只需要ignore the file locally有一段时间,你可以试试:
git update-index --assume-unchanged -- file3.txt
注意:这根本不涉及 .gitignore
。
停止本地忽略更改:
git update-index --no-assume-unchanged -- file3.txt