Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GIT, don't merge specific files

I have this kind of situation. I have 2 branches, master and develop.

On develop branch I have some file, lets say tools.js. If I need I change this file, commit it and push it to Github develop branch.

After everything is done I merge develop branch into master. There is no tools.js file on master branch and I want to keep it like that.

How do I skip some files during the merge?

like image 787
Igor-Vuk Avatar asked Oct 22 '25 22:10

Igor-Vuk


1 Answers

Since you only merge devlop branch into master branch (not opposite), then you can add .gitignore file to ignore tools.js in master branch only. Detail steps as below:

1. Add .gitignore to ignore tools.js in master branch

git checkout master
touch .gitignore
echo 'tools.js' > .gitignore
git add .gitignore
git commit -m 'ignore tool.js in master branch'

Note: If you have already committed the tools.js file in master branch, then use below commands to ignore the file in HEAD version:

git rm tools.js --cached
git add .
git commit -m 'remove tools.js in HEAD version on master branch'

2. Delete tools.js during merging develop into master branch

After ignoring tools.js file in master branch, you can merge develop branch into master branch. And there will has merge conflict for the file tools.js during merging. And you can use below commands to resolve merge conflicts:

git rm tools.js
# modify and save other conflict files if there has
git add .
git commit -m 'merge develop branch into master branch'
like image 180
Marina Liu Avatar answered Oct 24 '25 12:10

Marina Liu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!