简体   繁体   English

如何在回购中获取文件忽略本地更改

[英]How to get files on repo ignore local changes

I have several files on my bitbucket repo that are config files, however it contains the config information to go live however a entirely different set of rules is used for local configs, however I have messed up a few times and added the changed rules and pushed them and its become a problem. 我的Bitbucket存储库中有几个文件是配置文件,但是其中包含要上线的配置信息,但是本地配置使用了完全不同的一组规则,但是我搞砸了几次,添加了更改的规则并推送了他们和它成为一个问题。

Is there any way of having a set file on the repo and it cant be overwritten or touched/added or pushed. 有什么办法可以在仓库中放置文件,并且不能覆盖,触摸/添加或推送文件。

Similar to the git update-index --assume-unchanged 类似于git update-index --assume-unchanged

There is not a reliable way to keep a file in the repo while ignoring local changes to that file. 没有一种可靠的方法将文件保留在存储库中,而忽略对该文件的本地更改。 The best solution is to arrange your build process so that the local differences are introduced either (a) outside your work tree, or (b) in files that you don't keep in source control, and can therefore exclude with .gitignore . 最好的解决方案是安排构建过程,以便(a)在工作树之外引入局部差异,或者在(b)不在源代码控制中保留的文件中引入局部差异,因此可以使用.gitignore排除。

For example, suppose you have a file app.config . 例如,假设您有一个文件app.config It needs one set of values in local builds and a different set of values on a production server (or possibly even more sets of values in different server environments). 它需要本地构建中的一组值和生产服务器上的一组不同值(或在不同服务器环境中甚至可能需要更多组值)。 Suppose the current copy in source control looks like this: 假设源代码控制中的当前副本如下所示:

db-server = devdb.mydomain.com
db-user = svcDevDb
db-password = s3cr3t

You could do the following: 您可以执行以下操作:

  • Remove app.config from commits going forward: git rm --cached app.config 从以后的提交中删除app.configgit rm --cached app.config
  • Add app.config to your .gitignore file app.config添加到您的.gitignore文件
  • Create a new file, app.config-template , which contains placeholders for values that would differ between environments 创建一个新文件app.config-template ,其中包含占位符,表示在环境之间会有所不同的值

This might look like 这可能看起来像

db-server = ${db-hostname}.mydomain.com
db-user = ${db-user}
db-password = ${db-password}
  • Create one or more "property files" containing shared sets of values that can be plugged into the template 创建一个或多个“属性文件”,其中包含可插入模板的共享值集

... ...

$ mkdir propfiles
$ cat > propfiles/dev
db-hostname = devdb
db-user = svcDevDb
db-password = s3cr3t
  • Now you'll need a script that, given the name of a property file, generates your app.config by filling out the template; 现在,您将需要一个脚本,该脚本具有属性文件的名称,可通过填写模板来生成app.config and that becomes part of your build process. 这将成为您构建过程的一部分。 (It may seem like I'm glossing over the hard part here, but any decent build framework should either do this for you, or have a plugin that does.) (这似乎让我难以理解,但任何体面的构建框架都应该为您执行此操作,或者拥有一个可以执行此操作的插件。)

So you add these changes and commit them. 因此,您add这些更改并commit Now when you build for the dev environment, you specify that values should be taken from propfiles/dev . 现在,当您为dev环境进行构建时,可以指定应从propfiles/dev获取值。 For local builds, you can create your own local file; 对于本地构建,您可以创建自己的local文件。 you could store it outside the work tree, or you could add propfiles/local to .gitignore . 您可以将其存储在工作树之外,也可以将propfiles/local添加到.gitignore You might create a local-defaults propfile for source control, with the intent that people will copy it and edit the copy to make their own local propfile; 您可能会创建用于源代码控制的local-defaults属性文件,目的是人们将其复制并编辑副本以制作自己的本地属性文件。 but the point is local changes don't get made directly to files that are in source control. 但是关键是本地更改不会直接对源代码控制中的文件进行。

This also means that your production propfile could be kept out of source control, and maintained in a secure environment (eg a build server that not everyone has full access to). 这也意味着您的生产属性文件可以不受源代码控制,并保持在安全的环境中(例如,并非每个人都可以完全访问的构建服务器)。 In our example, the config file contains a password, so being able to keep the production password (and your personal one from your local file) out of source control is worth possibly even more than avoiding the hassle of accidental commits of local config changes in this case. 在我们的示例中,配置文件包含一个密码,因此,使生产密码(以及您本地文件中的个人密码)不受源代码控制的好处,甚至比避免在本地意外更改本地配置更改的麻烦更有价值。这个案例。

There are other solutions, and the details can always vary; 还有其他解决方案,细节总是可以变化的。 but the point is, it's better to address as a build problem than a source control problem. 但是重点是,比起源代码控制问题,解决作为构建问题要好得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM