I have a struggle with git about a case. I want to keep track of my project on a GitHub repository with two branches main and dev. However, there are two files, and I don’t want to change them when merging between branches because these files keep branch-specific information (e.g. connection string).
Step-by-step I follow the strategy below:
1. Clone remote repository
There is only main branch on remote repository.
2. Configure merge driver
git config --global merge.ours.driver true
git config merge.ours.driver true
3. Added .gitattributes file to the root folder and filled as follows
echo 'Project/appsettings.json merge=ours' >> .gitattributes
echo 'Project/Program.cs merge=ours' >> .gitattributes
4. Commit and push .gitattributes to the main branch
git add .gitattributes
git commit -m ".gitattributes added"
git push origin main
5. Checkout to the dev branch and edited files
git checkout -b dev
After that, I changed the connection string and other branch-specific information in those two files in .gitattributes, and in some other files. Then, I committed and pushed changes to the remote dev branch.
6. Checkout to the main branch again and merge dev to main
Now, I want here that all files need to be merged besides files in .gitattributes. For this, I used several options. After the following command, all edited files are changed. Even the files included in .gitattributes.
git merge dev
The output of the following command is “Merge made by the ‘ours’ strategy.“. However, none of the files are changed. Even the files not included in .gitattributes.
git merge -s ours dev
I read many solutions, but I could not solve the problem and understand what’s the issue. I highly appreciate your time and help regarding this problem. Thanks in advance!
1
because these files keep branch-specific information (e.g. connection string).
I think you’ll need to rethink that instead of trying to wrangle Git to do what you want.
Have branch-specific settings in branch-specific files, and tell your app to read the specific file depending on the branch it detects itself to be on (or more explicitly with e.g. a command-line switch, environment variable, etc.).
I couldn’t solve the problem using .gitattributes. Basically, my solution is merging without commit and fast-forward. Then, reverting those files using HEAD.
git merge —-no-commit —-no-ff <branch>
git checkout HEAD —- <file>