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

蹲街弑〆低调 提交于 2019-12-02 02:22:07

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.

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!

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

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