How can I merge in Git without changing config files in master?

做~自己de王妃 提交于 2019-12-31 03:28:10

问题


We have a number of configuration files that contain information, such as a url, that need to be modified for our developer environment. We create the developer environment as a branch in git, and make the changes to the configuration files. The problem is that when we merge the development branch back to the master branch, it changes the configuration files in the master branch.

We would like to store the files in git, so I do not believe we can use .gitignore. We need to keep all of our source in the repo since we are using elastic beanstalk, and need to deploy the entire source each time.

We have tried to use .gitattributes, and read many of the posts, such as How do I tell git to always select my local version for conflicted merges on a specific file?, but it appears that the merge driver that you setup in .gitattributes will only be called if there is a change to both files. In our case we will not be changing the configuration files in the master branch, so the merge driver is never called.

Is there an easy way to do this without writing a script?


回答1:


Solution is simple: Do not commit the configuration file.

Create a template config file and commit it. When installing your application, copy the template and change what you need to change. Keep the real config file in .gitignore to avoid accidental commits.

Advanced use is to create two config files: application defaults and local overrides. Defaults are committed, local overrides are not committed. Benefit of this approach is that you override only what you need to and leave other values unspecified, so when default value changes, you don't have to update your local override config. Downside is that you have to load and merge two config files during the initialization of your application.




回答2:


After researching a number of git commands such as rebase and fetch, I found a simple way to do this. Just create the branch as an orphan, copy the config files to it, and use the command "git checkout configs_branch -- . " to essentially copy the config files to the parent branch.

So in detail:

initialize the git repo with the repo url

git remote add origin (remote repository url)

make sure you are in the branch you want to create the branch from

create the branch that will contain the config files as an orphan branch so that the branch does not contain all of your source

git checkout --orphan configs_branch

copy the config files to this branch of the repo add the files, commit, and push

Git add .

Git commit -m "initial commit with config files"

git push origin configs_url

AFTER A SUCCESSFUL MERGE

from parent branch

git checkout parentbranchname

git fetch --all

copy the files from the config branch to the parent branch

git checkout configs_branch -- .

(you will still be on the parent branch after the above command)

commit the files

git commit -m "resetting the config files"

Thanks @vampire for your help!




回答3:


You asked for solutions without the necessity of writing a script. My solution includes scripts. However, they are already written.

1) Create a custom merge driver that will ignore merge conflicts for the respective file. The driver has to be registered for each file in .gitattributes

2) Create merge conflicts at each commit in that file (otherwise, the merge is trivial and the merge driver will not be used). This can be done with a post-commit hook in .git/hooks/post-commit that changes that file in each branch after each commit.

I combined that in some scripts: https://github.com/keepitfree/faithfulgit



来源:https://stackoverflow.com/questions/40026194/how-can-i-merge-in-git-without-changing-config-files-in-master

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