2

I am trying to create a workflow with Git for my personal projects. After reading this Post, A successful Git branching model, I am planning to implement the same in all my projects. The master branch will contain the production code and the develop branch which I use as working branch.

So consider the following directory structure.

bower_components
node_modules
dist
gulpfile.js
index.php
assets
  -css
  -js
  -images
package.json
bower.json
.gitignore
README.md

Obviously, I don't want the bower_components and node_modules to be pushed to my develop and master branch, hence I add it to .gitignore. So the other developers can checkout the develop branch and run npm install and bower install to continue development.

I would like to keep my master(release) branch clean where I don't need package.json, gulpfile.js, bower.json etc.

So the master branch must contain only the below diretories

dist
index.php

dist will be the minified js and css. I want to achieve the above scenario using Git. Either I need different .gitignore for master branch and develop branch or ignore files during git merge.

I have read several SO questions, checkout this question which explains a way to ignore files for branches using excludeFiles option. I have tried it but it doesn't seems to be working. This is the link for the mentioned solution. And this SO answer explains that

Git does not support per-branch excludes files

How can I achieve this above scenario using Git? Which option should I use?

Community
  • 1
  • 1
Vishnu Sureshkumar
  • 2,246
  • 6
  • 35
  • 52

3 Answers3

1

If you add the .gitignore to the repository, you could have different versions in different branches. But then you couldn’t merge the branches, or the change would carry over to the other branch.

However, you can simply ignore the files, and add the ignored files to the repository anyway. You can simply run git add --force to add files even if they are ignored. Note that this will not help you figure out when there are new files in the ignored folders; you have to add those manually yourself, and Git will not be able to tell you that there are new files.

Otherwise, this is simply not possible, and I would argue that you shouldn’t have things like minified CSS in the repository. Everything that can be generated from the source shouldn’t be part of the repository.

poke
  • 369,085
  • 72
  • 557
  • 602
  • Thanks for the answer. I am looking for an option which is more reliable. For this I have to manually stage the files every time and as you mentioned there is a chance for me to forget about a change or new file in ignore folder. I just want to keep the master branch for distribution and so my client just has to download and use it, who is not aware of dependency management and building production code. – Vishnu Sureshkumar Aug 22 '15 at 12:33
  • Git repositories are not made for just downloading a distribution version. When you clone a repository, you always get the *full* history of *all branches*, so it’s not suitable for just distributing one version that can be used by a client. Instead, package the software, and provide a download as an archive somewhere. – poke Aug 22 '15 at 12:35
  • That makes sense. Forget about the scenario, is there any way to ignore files per branch. ? – Vishnu Sureshkumar Aug 22 '15 at 15:15
  • No. Ignoring only works based on `.gitignore` which may be part of the repository, or based on other branch-independent ignore files. – poke Aug 22 '15 at 15:18
0

I would strongly recommend to put files as packages.json and bower.json to the master release branch. The deployment and continuous integration system will need them. Also anybody who forks the master branch will need them.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
0

As some comments already suggest, I think you have misinterpreted the purpose of the master branch, which is hold a stable version of the source and not to have a "compiled" version of of your stable code. In addition it's not a good idea to have those "compiled" files tracked under git.

It's easier automate a deployment process, which will do all the tasks needed to prepare to code for the deployment, including the clean of unwanted files. In this way you can forget about forgetting erase some of the files needed to "compile". Also have in mind that git it's not prepared for ditribution to the end users.

nelsonx64
  • 11
  • 1