忽略Git中特定文件中的一行 [英] Ignore one line in a specific file in Git

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

问题描述

我有一个文件database.properties,其中有

        password = something

因此,每当我必须进行测试时,都必须将此行更改为本地密码.团队中的每个人都将拥有自己的本地密码.

So whenever I have to test something I will have to change this line to my local password. Everybody in the team will have their own local password.

我想我必须执行以下操作之一:

I guess I will have to do one of the following :

  1. 每次提交时,我都不应该将database.properties添加到 索引(我不想ignore此文件),但是当有 许多文件很容易错过此文件.

  1. Every time I commit I should not add database.properties to index (I don't want to ignore this file), but when there are too many files it's easy to miss this file.

在提交之前撤消更改,但是大部分时间我都忘记了 为此.

Undo the changes before committing, but most of the time I forget to do this.

创建配置文件可能是实现此目的的一种方法,但甚至会遇到与database.properties类似的问题.

Creating a profile may be a way to achieve this but even will have similar issues as database.properties.

我已经检查了 git可以忽略特定行吗?如何告诉git忽略单独的行,即gitignore用于特定的代码行.

暂时忽略文件,我看到了我们可以做到

From temporarily ignoring files I see that we can do

git update-index --assume-unchanged <file>

但是我每次都必须这样做.

but I will have to do this every time.

以上所有参考文献都是在3-4年之前问/写的.所以我只想知道是否有更好的方法可以忽略这一行.

All the above references were asked/written before 3-4 years. So I just want to know If there is a better way to ignore just that one line.

编辑

按照@VonC的回答,我尝试在.git/info/.gitattribute

Following @VonC's answer I have tried to add a filter in .git/info/.gitattribute

{directory_path}/database.properties filter=password // I have given the correct directory path.

我将smudgeclean脚本添加为:

git config --global filter.password.smudge ~/smudge.sh 
git config --global filter.password.clean ~/clean.sh 

(当我运行~/smudge.sh~/clean.sh时,它将正确替换password= something行.因此脚本是正确的.)

(When I run ~/smudge.sh and ~/clean.sh it replaces password= something line correctly. So the scripts are correct.)

但是当我添加/提交database.properties时,似乎对database.properties文件没有影响. (我猜想~/clean.sh在将其添加到索引时应该可以运行)

But when I add/commit the database.properties, there seems to be no affect on database.properties file. (I am guessing ~/clean.sh should run when I add it to index)

我做错了什么?

推荐答案

我坚持2014年的回答" git可以忽略特定的内容吗?行吗?:制作了git内容过滤器,可以用另一种内容来更改文件的一行.

I stand by my 2014 answer in "Can git ignore a specific line?": a git content filter is made to change a line of a file by another content.

它仍然需要一个(一次)git config命令才能在您的仓库中声明该过滤器,但是它将保持活动状态(与

It still requires a (one time) git config command in order to declare that filter in your repo, but it will remain active (as oppose to a git update-index, which can be discarded on, for instance, a git reset)

过滤器与.gitattributes文件中的文件关联,该文件与database.properties放在同一文件夹中. (请参阅讨论)
它与要在存储库的本地配置中执行的程序相关联.

The filter is associated to files in a .gitattributes file, put in the same folder as the database.properties. (see discussion)
It is associated to a program to execute in the local config of the repo.

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

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