4

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?

Igor-Vuk
  • 3,551
  • 7
  • 30
  • 65
  • 2
    You can't, not without a lot of work. Fortunately this is likely an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Could you explain to us what this file is, why it's in `develop`, and why you don't want to merge it into `master`? Then we can come up with a better way to handle the problem. – Schwern Jun 18 '18 at 18:50
  • I am deploying app to Heroku. I build everything locally on develop, commit it and push it to bitbucket where pipelines take over and deploy it to Heroku. If everything is ok, I merge develop into master and push the master. The thing is that all the files that got build by me locally before pushing to Heroku get also merged into the master. There is no need for that. – Igor-Vuk Jun 18 '18 at 18:59
  • 2
    Are these build artifacts? Should they be checked in at all? Maybe they need to be in your [.gitignore](https://git-scm.com/docs/gitignore). – Schwern Jun 18 '18 at 19:08
  • They need to be in gitignore on my master if someone wants for example clone the project. He doesnt need those builds. They must not be in develop branch since I am pushing this builds to Heroku. – Igor-Vuk Jun 18 '18 at 19:14
  • Do they need to be checked in on `develop`? If not, add them to `.gitignore` on `develop` and [tell git to forget about the already checked in files](https://stackoverflow.com/questions/1274057/how-to-make-git-forget-about-a-file-that-was-tracked-but-is-now-in-gitignore#1274447). – Schwern Jun 18 '18 at 19:33
  • Sorry. I am not following. This build files are rescreated whenever I change something in my code.They can not be in .gitignore because they must be commited in order for Heroku to take them when i push to it. – Igor-Vuk Jun 18 '18 at 21:15
  • 2
    I see. I think that's unusual. Normally you have Heroku perform the build. I think you have a problem with your Heroku deployment. You should ask about that instead. – Schwern Jun 18 '18 at 21:36
  • I don't like when Heroku does the build, all the webpack bundling, minification gziping and so on I automated locally and I am just pushing the bundled file to Heroku to do it's thing. I have much more control like that. This bundle file is not needed on master. If someone wants it can easily run srcipt `yarn run build` – Igor-Vuk Jun 18 '18 at 22:16
  • 2
    I think you should ask a question about your deployment preferences and how to make them work with Heroku and Git. There are things you can do to excluding certain files from a merge, but they're not pleasant and should not be part of your regular workflow. There's definitely a better solution for you, but I'm not a Heroku expert. – Schwern Jun 18 '18 at 22:54
  • @lgor-Vuk Does it only merge `develop` branch into `master` branch not merge the opposite (merge `master` branch into `develop` branch)? – Marina Liu Jun 19 '18 at 02:28
  • yes, I only merge develop (or some other branches) into master. Master branch is my last station where I push production ready code to github, I don't merge master into anything. – Igor-Vuk Jun 19 '18 at 10:04

2 Answers2

2

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'
Marina Liu
  • 36,876
  • 5
  • 61
  • 74
0

As you’ve described things, tools.js is a build artifact, so it has no place in your Git repo at all. You’ll want to build it somewhere in your deploy process—if not on Heroku, then in Pipelines.

Marnen Laibow-Koser
  • 5,959
  • 1
  • 28
  • 33