简体   繁体   中英

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.

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

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 .

For example, suppose you have a file 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
  • Add app.config to your .gitignore file
  • Create a new file, app.config-template , which contains placeholders for values that would differ between environments

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; 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. Now when you build for the dev environment, you specify that values should be taken from propfiles/dev . For local builds, you can create your own local file; you could store it outside the work tree, or you could add propfiles/local to .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; 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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