如何停止跟踪并忽略对 Git 中文件的更改? [英] How to stop tracking and ignore changes to a file in Git?

查看:29
本文介绍了如何停止跟踪并忽略对 Git 中文件的更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我克隆了一个包含一些 .csproj 文件的项目.我不需要/喜欢我的本地 csproj 文件被 Git 跟踪(或在创建补丁时被调出),但显然项目中需要它们.

我已将 *.csproj 添加到我的 LOCAL .gitignore,但文件已在存储库中.

当我输入 git status 时,它会显示我对 csproj 的更改,我对跟踪或提交补丁不感兴趣.

如何从我的个人存储库中删除这些文件的跟踪"(但将它们保留在源中以便我可以使用它们),以便在执行状态(或创建补丁)时看不到更改)?

是否有正确/规范的方法来处理这种情况?

解决方案

只需对要从修订控制中删除的每个文件调用 git rm --cached 应该没问题.只要您的本地忽略模式正确,您就不会在 git status 的输出中看到这些文件.

请注意,此解决方案会从存储库中删除文件,因此所有开发人员都需要维护自己的本地(非修订控制)文件副本

为了防止 git 检测到这些文件中的更改,您还应该使用以下命令:

git update-index --assume-unchanged [路径]

您可能想要做什么:(从下面@Ryan Taylor 回答)

<块引用>

  1. 这是告诉 git 你想要你自己的文件或文件夹的独立版本.例如,您不想覆盖(或删除)生产/暂存配置文件.

git update-index --skip-worktree <路径名>

完整答案在此 URL 中:http://source.kohlerville.com/2009/02/untrack-files-in-git/

I have cloned a project that includes some .csproj files. I don't need/like my local csproj files being tracked by Git (or being brought up when creating a patch), but clearly they are needed in the project.

I have added *.csproj to my LOCAL .gitignore, but the files are already in the repo.

When I type git status, it shows my changes to csproj which I am not interested in keeping track of or submitting for patches.

How do I remove the "tracking of" these files from my personal repo (but keep them in the source so I can use them) so that I don't see the changes when I do a status (or create a patch)?

Is there a correct/canonical way to handle this situation?

解决方案

Just calling git rm --cached on each of the files you want to remove from revision control should be fine. As long as your local ignore patterns are correct you won't see these files included in the output of git status.

Note that this solution removes the files from the repository, so all developers would need to maintain their own local (non-revision controlled) copies of the file

To prevent git from detecting changes in these files you should also use this command:

git update-index --assume-unchanged [path]

What you probably want to do: (from below @Ryan Taylor answer)

  1. This is to tell git you want your own independent version of the file or folder. For instance, you don't want to overwrite (or delete) production/staging config files.

git update-index --skip-worktree <path-name>

The full answer is here in this URL: http://source.kohlerville.com/2009/02/untrack-files-in-git/

这篇关于如何停止跟踪并忽略对 Git 中文件的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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