Git - 忽略对配置文件的特定修改 [英] Git - Ignoring a specific modification to a config file

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

问题描述

我在我们的项目中有一个配置文件,它包含一个连接字符串到数据库以及几个应用程序设置,例如:

 < setting name =ConnectionString>< value> Server = prodServer; Database = myDataBase;< / value>< / setting> 
< setting name =AnotherSetting1>< value> Lorem< /值>< / setting>
< setting name =AnotherSetting2>< value> Ipsum< /值>< / setting>
< setting name =AnotherSetting3>< value> dolor< /值>< / setting>
...

在我的开发过程中,我总是更改 ConnectionString value到我的本地数据库,这会导致git将文件标记为已修改。



我阅读了关于执行以下操作的选项:



git update-index --assume-unchanged app.config



<但是,这意味着如果我对文件做任何其他更改(例如,更改 AnotherSetting1 ),那么git也会忽略它。



是否有办法告诉git忽略文件中的特定更改,但如果发生其他更改,则将其标记为已修改?或者是否有另一个类似的解决方案来解决这个问题?



请假定我不能对配置文件本身的体系结构进行任何更改 - 我想要一个local-只有解决方案。

解决方案

过滤器 是为类似这样的内容制作的。在您的回购中,

  cat> .git / info / saved-connection  

设置name =ConnectionString>< value> Server = prodServer; Database = myDataBase;< /值>< / setting>
EOD

cat> .git / info / my-connection<< EOD
< setting name =ConnectionString>< value> Server = myprivateserver ;数据库= MYDATABASE;< /值GT;< /设定>
EOD

git config filter.use-my-connection.smudge'sed -f.git / info / use-my-connection.smudge'
git config filter .use-my-connection.clean'sed -f.git / info / use-my-connection.clean'

cat> .git / info / use-my-connection.smudge <<<< EOD
/ ^< setting name =ConnectionString> / {
w .git / info / saved-connection
r .git / info / my-connection
d
}
EOD

cat> .git / info / use-my-connection.clean<< EOD
/ ^< setting name = ConnectionString> / {
w .git / info / my-connection $ b $ .git / info / saved-connection
d
}
EOD

回声>> .git / info / attributes path / to / app.config filter = use-my-connection


I have a config file in our project that holds a connection string to the database, as well as several application settings, e.g:

...
<setting name="ConnectionString"><value>Server=prodServer;Database=myDataBase;</value></setting>
<setting name="AnotherSetting1"><value>Lorem</value></setting>
<setting name="AnotherSetting2"><value>Ipsum</value></setting>
<setting name="AnotherSetting3"><value>dolor </value></setting>
...

During my development, I always change the ConnectionString value to my local database which causes git to put the file mark the file as "modified".

I read about the option of doing the following:

git update-index --assume-unchanged app.config

However, that would mean that if I do ANY other change to the file (e.g, change AnotherSetting1) then git would ignore that as well.

Is there a way to tell git to ignore a specific change in file, but to mark it as modified if any other changes occur? Or is there another similar solution to this problem?

Please assume that I can't make any changes to architecture of the config file itself - I'd like a local-only solution.

解决方案

filters are made for stuff like this. In your repo,

cat >.git/info/saved-connection <<EOD
<setting name="ConnectionString"><value>Server=prodServer;Database=myDataBase;</value></setting>
EOD

cat >.git/info/my-connection <<EOD
<setting name="ConnectionString"><value>Server=myprivateserver;Database=myDataBase;</value></setting>
EOD

git config filter.use-my-connection.smudge 'sed -f ".git/info/use-my-connection.smudge"'
git config filter.use-my-connection.clean  'sed -f ".git/info/use-my-connection.clean"'

cat >.git/info/use-my-connection.smudge    <<EOD
/^<setting name="ConnectionString">/ {
     w .git/info/saved-connection
     r .git/info/my-connection
     d
}
EOD

cat >.git/info/use-my-connection.clean     <<EOD
/^<setting name="ConnectionString">/ {
     w .git/info/my-connection
     r .git/info/saved-connection
     d
}
EOD

echo >> .git/info/attributes     path/to/app.config filter=use-my-connection

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

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