dsxz84851 2014-03-14 12:24 采纳率: 100%
浏览 1341

如何从git merge中排除特定的提交(永远)

I'm developing some web application (php, codeigniter). Today a time to push it to github has come, but I've encountered an issue I can't manage.

I've got 2 branches:

  • master - this branch should be used for github pushing, does not contain credentials or any other sensitive data (just public code)
  • dev - this is master's child, used for development, does contain credentials (of my private app instantion)

By credentials I mean: database login, password, host, user; oauth services id / secrets; encryption_key. Credentials are stored in 2 separated files within /application/config folder.

The idea is that I'll make all changes on dev, than merge dev into master, and pull master content to github (but master can't contain my private credentials).

This is what I've done till now:

  1. Removed all credentials from /application/config/config.php and /applications/config/database.php (as I don't want them to be public on github) they've become an empty strings like: $db['default']['hostname'] = '';
  2. Started git repository and made an initial commit - the default branch that was created is named 'master'
  3. Pulled master into github

And it's pretty cool till now - I've got my project code on github with no credentials within. Next I need to make some changes in my project, so I:

  1. Created new branch 'dev'
  2. Stored my credentials into dev, and make a commit - I need dev instantion to have credentials as I'm uploading this to ftp server every time I change something in the code (I don't use local LAMP or anything like this in this project)

So now I need to make some change in dev, upload it and pull to github so I:

  1. Make an change on dev (let's say add new feature)
  2. Checkout to master
  3. Merge dev into master.

What I expected to happen: Git will ask me which version of configs should be used in 'master' as it is different in both branches. I will use diff to chose a 'master' verion of files (which does not contain credentials). Git will remember my choice, and in the future I will not have to chose again when I'll merge dev into master. Git works like this on one of my other projects, where dev is a master branch and instantions are it's children (In this case dev is a child and I merge child into parent).

What happened: Git just copied credentials from dev to master, did not ask me about that. So now my master branch is 'tainted' by credentials and I can't push it to github anymore.

My questions are:

  1. Why is that happening, how can I avoid this?
  2. Is there any other (better?) way to handle that kind of issue.

Please let me know if anything is unclear, any help will be appreciated.

  • 写回答

3条回答 默认 最新

  • doudui5753 2014-03-14 12:32
    关注

    Git will only flag a conflict if files in your master branch were changed after your original branch to dev, then the same files were changed in dev.

    I suggest you add a .gitignore entry for your credentials, or separate them using an ENVIRONMENT variable, in codeigniter this is very easy, all you have to do is on your apache config (vhost if your using them) is add SetEnv APPLICATION_ENV development then you can add a folder inside application/configs called development, now when you are running in development environment, these configs will take precedence over the ones in the base config directory.

    This isn't so much about gitignoring files, or removing commits, but a good file structure, and environment setup to differentiate between the separate environments, you an use development, staging, and production (which is the default)

    otherwise add a .gitignore row to tell git to ignore those certain files.

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值