Search code examples
gitversion-controlfilesystemsbranchgitignore

Git - Multiple branches with different file systems


How to correctly organize multiple branches with each having different file system?

Here is the situation -


I have two branches:

  • Current master with project files that were made a year ago
  • Current develop with new project files (completely different framework) that is about to develop and at one moment switch


The point where things get problematic is that develop brach has, for example, folder "storage" which is ignored by .gitignore file. Other branch does not have this folder (and that is why it is not in its .gitignore file as well). When I checkout from develop to master brach, this "storage" folder suddenly appears in other branch where it should not be at all.

How do I specify each project which files/folder must no be sent to server and at the same time which files/folder must stick with each branch without travelling through branches on each checkout.

I managed to solve this issue by adding this folder to .gitignore file in project where this folder should not even exist (that is how I don't see that I have any unchanged files that should be commited) but I guess it is not the best solution because why would I exclude something that should not even be there?

It get's even more complicated when I am using PHPStorm IDE. PHPStorm creates in each project (in this case - each branch) ".idea" folder where are located all config and history files for specific project. Obviously, I don't want to send this folder to remote repository (because each developer has its own .idea folder for each project), that is why I exclude it in .gitignore file (I did this for both branches because each branch has its own ".idea" folder). When I checkout to another branch, this folder travels to that branch which should not happen because this .idea folder ruins other branche's .idea folder. All configs are messed up.

Why two different project file systems as two branches not two projects? - Because it is the same project. Just completely new version which serves the same visual design, functionality and adds a lot of extra functionality. It makes no sense to call this as some other project. It is different version but the same project.


Solution

  • If you ignore files / folders with .gitignore, they are ignored by Git, fullstop. They are not tracked and are not mangled with, they are simply ignored. They are not added, modified or deleted, but fully ignored. They are not assigned to any branch, as they are ignored.

    I think in your case it would be the best to have multiple worktrees for the different branches. A worktree is the directory where the actual files are checked out for you to work on. With git worktree you can add multiple worktrees to one repository, making it possible to have different branches checked out to different working directories.

    This way you can have folder a for branch a, where you have ignored .idea for branch a and folder b for branch b where you have ignored .idea for branch b.

    Actually most files in .idea (e. g. not the workspace.xml, but I think all other files) are not developer specific, but project specific. So everything except of workspace.xml is meant to be able to being checked in into version control, so that if you switch to a different branch, you have the correct IDE metadata for this branch readily available and just have /.idea/workspace.xml added to .gitignore as this file contains developer specicific stuff like non-shared run configurations, opened files, tool window layout, window positions, and so on. So this might be another solution for you to consider.