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?

user31782
  • 7,087
  • 14
  • 68
  • 143
  • 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 '16 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. – Jeff Puckett Oct 27 '16 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 '16 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 '16 at 01: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? – Firas KADHUM Oct 20 '22 at 14:16
  • @FirasKADHUM In these cases, the repositories are *not the same*. Hence, each of them can have a different branch checked out. – j6t Oct 21 '22 at 07:28

3 Answers3

23

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
kaitoy
  • 1,545
  • 9
  • 16
10

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.

j6t
  • 9,150
  • 1
  • 15
  • 35
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.

Community
  • 1
  • 1
Tom
  • 1,387
  • 3
  • 19
  • 30
  • 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 '16 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 '16 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 '16 at 12:47
  • @user31782 I updated the answer considering your doubts. – Tom Oct 27 '16 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 '16 at 12:51
  • I have to copy. Because I have a responsive.css file in different location in pc. I want this to add only to one branch -- dev. I also want to work cuncurrently in master too where there shouldn't be any responsive.css.. but repsonsive.css file appear in the whole repository folder which applies on my html files in master branch too. – user31782 Oct 27 '16 at 12:56
  • Hey, it seems like files are disappeared magically when I checkout to other branch. but are files deleted and downloaded again when I checkout? Or are just put to recycle bin? – user31782 Oct 27 '16 at 13:00
  • You're working with branches, it doesn't have anything to do with Recycle Bin. Just checkout the other branch and the files must be there. – Tom Oct 27 '16 at 13:04
  • But were they deleted from the computer? I have a `readme.md` file in `dev` but nothing in `master`. Now when I checkout to `master` the repository folder doean't have `readme.md` file anymore. Where is that `readme.md` file in the computer now? If git deletes and redownloads files on each checkout then that is important to me. – user31782 Oct 27 '16 at 13:10
  • You're very new to git, right? Try to follow this to have a better understanding: https://www.codeschool.com/courses/try-git. I don't know the steps you did. I don't know where you're versioning (github, bitbucket or just in your local machine), but you must have a better understanding of how `git` or a SCM works. Your files could be committed in one of your branches, I don't know. Try `git branch -v` to see how many branches you have. Having the branch list, try the `git checkout ` to look for you files. – Tom Oct 27 '16 at 13:19
  • I know about git. I have been working with github gui. But for this project I nneded to make private repository; which is possible with bitbucket. But bitbucket gui is ugly. So I just started using gitbash. I have to branches `dev` and `master`. `Master` is empty. `dev` has three folders. when I checkout to `master` suddenly all the three folders disappear from my computer. Although this is what I want and now I do not need seperate branches in seperate folders. But where did those three folder go when I checked out? – user31782 Oct 27 '16 at 13:25