问题
I have a web project with two git branches (developing and production) and each one connects to a different server (dev and prod). Each branch has a set of tracked config files, such as different URLs, database options, sql exported files, etc. which are different on both branches.
Every time I try to merge those branches I get a lot of conflicts in these settings files which need to be resolved manually, but every now and then I miss something. In short, it is a nightmare. My question is therefore, how can I avoid this?
- Is there a way to prevent any changes to those config files while merging or rebasing?*
Suggested Solution: "Try to make those files untracked". I tried this but whenever I pulled from the production file I get those files deleted and replaced, so I don't know if I should do it in a different way.
回答1:
The way to do this is to register a merge strategy as per-file attribute that will always use the current version. Here is how to do this (the idea is from this blog)
First you register a merge driver that
- does nothing and
- exits successfully.
The shell command true
is exactly what we are looking for. The following line registers true
as the alwaysours
merge driver:
git config --local merge.alwaysours.driver true
Next, we need to tell git for which files to use this bogus merge driver. This is what the .gitattributes
file is for. It supports the same small set of file patterns as the .gitignore
file. For example
/configs/* merge=alwaysours
*.conf merge=alwaysours
would use our bogus merge driver for all files in '/config' or all files ending with '.conf'. It makes sense to also add this file to your git repository
git add .gitattributes
so that changes to it are tracked. There is one caveat though: when you merge in a branch having the .gitattributes
file, the alwaysours
driver will also be used on this branch! To avoid any problems arising from that, add the alwaysours
merge driver for .gitattributes
on all branches.
echo '/.gitattributes merge=alwaysours' > .gitattributes
git commit .gitattributes -m 'Register alwaysours driver for .gitattributes'
Of course this will break proper handling of any other settings you have in .gitattributes
.
来源:https://stackoverflow.com/questions/30116096/how-to-prevent-tracked-config-files-from-being-changed-by-merges-in-git