Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have different branches in different folders in git?

I have recently created a repository. I made two branches in it, master and dev. Now when I copy files in the repositry's directory then the files are added to both the branches. But I only want to add files to dev branch and then after the work is done in dev then I will copy contents from dev's folder into master's folder. Unfortuanately both the branches are thought of as the same folder by git.

So how to have different branches in different folders in git?

like image 213
user31782 Avatar asked Oct 27 '16 11:10

user31782


People also ask

How do I create a folder in branches?

You can prefix the branch you want to create with the folder name or branch type you want. For example you can put a feature branch into a feature folder by making the branch name. This is an example of how the tree will look.


2 Answers

You may like git-worktree. This allows you to checkout different branches to different directories.

E.g.

git checkout master
git worktree add ../dev dev
like image 177
kaitoy Avatar answered Oct 06 '22 20:10

kaitoy


Judging from your comment

Suppose in a project there are two branches frontend and backend. Frontend branch contains only static html pages and backend branch contains dynamic php pages. I download frontend branch then as you say it will also contain backend's php files too.

you have grossly misunderstood the purpose of branches.

You do not use branches to separate your files depending on their purpose. For this, you usually place them in different directories.

You use branches to separate a project state from a different project state that needed some changes. Take a website that you have completed. It has a set of files for the frontend and the backend. You have both sets in a branch master, and it is working well and stable. Now you decide to make the theme customizable. This typically needs changes in both sets of files. But you do not want to extinguish the current state of your website. Hence you make a new branch devel and make the changes (on both sets of files) on the new branch. Now, by checking out master and devel, you can switch back and forth between the stable state of the website and the development state.

like image 26
j6t Avatar answered Oct 06 '22 22:10

j6t