Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent merging a file from master with Git

In another question it is recommended to use .gitattributes in order to keep the file tracked but not merged in different branch, but my use case below seems not working..

mkdir git
cd git
git init
echo "B" > b.txt
git add b.txt 
git commit -m 'Initial commit'

echo "b.txt merge=keepMine" > .gitattributes
git add .gitattributes 
git config merge.keepMine.name "always keep mine during merge"
git config merge.keepMine.driver "keepMine.sh %O %A %B"
git commit -m 'Ignore b.txt'

git checkout -b test # Create a branch
git checkout master # Back to master and make change
echo "Only in master" > b.txt
git commit -a -m 'In master'

git checkout test
git merge master # The change in b.txt is being merged...

Any idea? Thanks..

like image 731
Ryan Avatar asked Nov 14 '22 04:11

Ryan


1 Answers

I suspect that it did not work because there were no merge conflicts.

However, I can confirm that git merge master --strategy=ours works as expected.

This might be what you're looking for: How to tell Git to always select my local version

If you want to specify a specific merge strategy for a specific file, what you really need to do is to write a custom merge driver and specify in your repository configuration that you want that merge driver to be used as the default. See the link above on how to use it.

In short, the reason it does not work in your use case is because your use case is not a merge. It only portrays a bunch of changes on master with a temporary branch called test being created and then moved up to master later on. You never actually introduce a commit while you're on the test branch.

like image 192
Carl Avatar answered Nov 16 '22 00:11

Carl