8

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?

6
  • Within the same repository, you can't have one directory versioned on branch master, and at the same time have another directory versioned on branch dev and have both directories checked out. You can have only one branch checked out, and that will remove the other branch's directory.
    – j6t
    Oct 27, 2016 at 11:49
  • "Now when I copy files in the repositry's directory then the files are added to both the branches." This isn't true. I think you need to play with it some more. Oct 27, 2016 at 11:51
  • @JeffPuckettII By added I mean when I check git status on either master or dev then both the branches say you have unstaged files that need to be added before commit. I want the files copied to be showen for commit only in dev branch.
    – user31782
    Oct 27, 2016 at 12:14
  • 2
    Files are not "on a branch" until committed. Your new unstaged files may be there as you flip between branches, but if you actually git commit them into one, you will not see them in the other.
    – Mort
    Oct 28, 2016 at 1:36
  • @j6t, why not? since you can have it on two different pc? why not in two separate directories? like project/v1 where I have branch v1 checked out, and project/v2 with brachn v2 checked out? how does git in project/v1 know what's happening in project/v2? Oct 20, 2022 at 14:16

3 Answers 3

24

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
11

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.

0

Your question is a little unclear about exactly what you need, but git supports submodule, that allows you to have your repository with one sub-folder that is another repository.

This would allow you to change content just in one sub-folder of your repository, not affecting you root repository.

You could have two repositories, with two branches in each of them:

Repository main-project

main-master
main-dev

Repository library

library-master
library-dev

In the repositories:

- main-master
    - app
    - modules
    - lib  --> git checkout library/lib-master

and

- main-dev
    - app
    - modules
    - lib  --> git checkout library/lib-dev

This structure allows you to checkout main-master and have the correct version of lib sub-folder.

At the same time, if you clone the branch main-dev to another directory, it will bring only the correct content.

To checkout the root branch with the sub-branch, try: git clone <repo>:main-master --recursive

If this is what you need, you can see more about it in Git Documentation.

Or here on StackOverflow.

I'm not sure this is what you're looking for, but I hope it helps.

11
  • This is too complicated for me. What if I delete my local repository and clone remote master branch in C directory and clone dev branch in D directory?
    – user31782
    Oct 27, 2016 at 12:25
  • Then you will have them exactly the same in folders C and D. If you want them to be different, you must have two branches in your root repository, each of them pointing to a different hash from the submodule (sub-folder) - that is another repository.
    – Tom
    Oct 27, 2016 at 12:36
  • 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. Isn't there an easy way to copy only frontend html files from this downloaded repository?
    – user31782
    Oct 27, 2016 at 12:47
  • @user31782 I updated the answer considering your doubts.
    – Tom
    Oct 27, 2016 at 12:48
  • Once your using Git, you don't copy, you checkout another branch, where the other versions of files are, and then it has the same result.
    – Tom
    Oct 27, 2016 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.