Git忽略对提交文件的更改 [英] Git ignore changes to committed file

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

问题描述

我的存储库中有一个配置文件,该配置文件不应将更改提交给它.

I have a config file in my repo that shouldn't get changes committed to it.

问题是,当我对文件进行更改时,它们会被git status拾取.我尝试了各种方法来忽略对其所做的更改.

The problem is that, when I make changes to the file they are picked up by git status. I've tried various ways to ignore changes to it.

当我克隆存储库时如何自动忽略它,以使我对文件所做的任何更改都不会被git拾取?

How can it be automatically ignored when I clone the repo, so that any changes I make to the file shouldn't be picked up by git?

我尝试过的解决方案:

  1. 将文件添加到.gitignore.似乎不起作用.这是因为文件已经在索引上了吗?

  1. Adding the file to .gitignore. Doesn't seem to work. Is this because the file is already on the index?

使用git update-index --assume-unchanged path/to/file.似乎可以使用某些功能,但仅适用于我的克隆存储库.其他克隆对象需要应用相同的命令.

Using git update-index --assume-unchanged path/to/file. Seemed to work some what but is only local to my cloned repo. Others who clone need to apply the same command.

我尝试过寻找其他答案,但是其他解决方案似乎无法正常工作.我觉得这应该很简单,所以任何指导都将对您有所帮助!

I've tried looking at other answers to this but other solutions don't seem to work properly. I feel like this should be quite simple, so any guidance would be helpful!

推荐答案

我的存储库中有一个配置文件,该配置文件不应将更改提交给它.

I have a config file in my repo that shouldn't get changes committed to it.

然后...首先不要提交该文件.
提交该文件的通用版本,该模板用于生成实际文件(作为私有非版本文件,添加到您的.gitignore中).

Then... don't commit that file in the first place.
Commit a generic version of that file, a template used to generate the actual file (as a private non-versioned file, added to your .gitignore).

该生成将在git checkout上自动生成.

That generation would be automatic on git checkout.

首先,重命名您现有的文件:

First, rename your existing file:

git mv afileToIgnore afile.tpl
git commit -m "record template value file"

然后将afileToIgnore添加到.gitignore文件:git status将不再显示要添加/更改的文件.

Then add to afileToIgnore to a .gitignore file: a git status won't show it anymore to be added/changed.

现在,声明污点 内容过滤器驱动程序 它会自动重新生成该文件(由于它位于.gitignore中,因此会被忽略)

Now, declare a smudge content filter driver which, automatically, will re-generate that file (ignored, since it is in .gitignore)

smudge脚本(可以版本):YourScript

smudge script (which you can version): YourScript

copy aFileToIgnore.tpl aFileToIgnore

在版本化的.gitattributes中声明内容过滤器驱动程序:

Declare the content filter driver in a versioned .gitattributes:

echo 'aFileToIgnore.tpl config' >> .gitattributes

该污点"config"内容过滤器驱动程序需要由每个克隆该存储库的用户本地激活.

That smudge 'config' content filter driver needs to be activated locally by each user cloning that repo.

cd /path/to/repo
git config filter.config.smudge YourScript

最后一步是每个用户需要执行的唯一步骤,以便从每个git checkout上自动生成的实际配置文件中受益.

That last step is the only one each user need to do in order to benefit from the actual config file to be automatically generated on each git checkout.

克隆的其他人也需要应用相同的命令.

Others who clone need to apply the same command.

是的,但是在这里,如果没有,他们将根本没有配置文件.
这比使文件在不应该被修改时更容易被修改更为可见.

True, but here, if they don't, they won't have the config file at all.
That is more visible than having the file ready to be modified when it should not be.

这篇关于Git忽略对提交文件的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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