简体   繁体   中英

How to exclude a particular commit from git merge (forever)

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.

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.

Git ingore will help. Read this blog post for solution.

http://blog.pagebakers.nl/2009/01/29/git-ignoring-changes-in-tracked-files/

You can include the credentials from a configuration file and add that file to .gitignore .

The config file can consist of settings for the dev, preproduction and production environments.

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