Managing a framework with Git, ignoring changes to given files after first pull

笑着哭i 提交于 2020-01-02 16:24:08

问题


I'm using git to manage an extended CodeIgniter Framework. It's a clone of the current CI release with extra helpers, libraries ect.

I have many sites all using this framework and if I add a new helper method or fix a bug in one site I want to be able to easily update all the other sites without overwriting any of their custom files.

I wish to achieve the following workflow

  1. Create a new site directory git init to initialise a blank local git repo
  2. Link this with the remote framework repo git remote add origin git@github.com:username/framework_repo
  3. Pull a fresh copy of the remote framework git pull origin master
  4. Make changes to site files and commit them back to the remote repo git push origin master
  5. Pull these changes down to the other sites
  6. Repeat steps 4 & 5

Thats all fine, BUT:

  • Files like config.php and database.php should never be committed back to the remote repo as they are unique to each site.
  • However I want them to exist in the remote repo so on the first pull request the default files are downloaded to my local directory.
  • Further more if I do another pull from the remote repo to update the framework I do not want these files to be overwritten

Whats the best way to achieve this? Some .gitignore voodoo? I already use .gitignore to ignore files, but in this case its slightly different as I want to pull the file only on the first request.

I hope that makes sense.


回答1:


  • Files like config.php and database.php should never be committed back to the remote repo as they are unique to each site.

Put all the files that shouldn't be in the repo to the .gitignore file. That is hardly any vodoo, it's just a list of files that should be ignored.

  • However I want them to exist in the remote repo so on the first pull request the default files are downloaded to my local directory.

create dummy files like config.default.php and copy them after the first pull to the ignored name.

  • Further more if I do another pull from the remote repo to update the framework I do not want these files to be overwritten

done with the first to steps!




回答2:


  1. Add config.php to your .gitignore.
  2. Create the default/distribution/base configuration to use, config.php-dist
  3. Create a Git hook, post-receive, like below.
  4. The hook will execute after each git pull, but the copy will only be done when config.php does not exist.

post-receive example (tailor to your needs):

#!/bin/bash
[ -f 'config.php' ] || cp config.php-dist config.php



回答3:


I think what you want is a number of branches for each customised site and an extra upstream branch for the common files. It doesn't really make much difference where each branch lives (or if you've got multiple copies of it).

When you make a change to the common files, you can either make it directly on the common branch or create a checkin on one of the customised branches then cherry-pick it onto the common branch. You don't want to push changes to the common branch, because that will take the customisations as well as the fixes. It's safe to pull from the common branch to the customised branches, as any changes you've made will be merged in.



来源:https://stackoverflow.com/questions/3117988/managing-a-framework-with-git-ignoring-changes-to-given-files-after-first-pull

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!